Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

messaging.setBackgroundMessageHandler() not work #161

Closed
chipchipzero opened this issue Sep 8, 2017 · 5 comments
Closed

messaging.setBackgroundMessageHandler() not work #161

chipchipzero opened this issue Sep 8, 2017 · 5 comments

Comments

@chipchipzero
Copy link

chipchipzero commented Sep 8, 2017

This is my serviceworker code:

importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
  'messagingSenderId': "FIREBASE_MESSAGING_SENDER_ID"
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
// [END initialize_firebase_in_sw]

// If you would like to customize notifications that are received in the
// background (Web app is closed or not in browser focus) then you should
// implement this optional method.
// [START background_handler]
messaging.setBackgroundMessageHandler(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  // test custom notice
  const notificationTitle = 'Background Message Title';
  const notificationOptions = {
    body: 'Background Message body.',
    icon: '/images/favicon.png'
  };

  return self.registration.showNotification(notificationTitle,
      notificationOptions);
});

I'm using fcm ruby gem to send message: https://github.com/spacialdb/fcm

This is my code:

fcm = FCM.new("FIREBASE_CLOUD_MESSAGING_KEY")

payload = {
  data: {
    object: data[:object],
    type: data[:type],
    notification: {title: data[:title], body: data[:message]}
  }
}
fcm.send(DEVICE_TOKEN, payload)

This is the result:
notification

@gauntface
Copy link

@thanhlongmuah do you see anything in your console - specifically the message "[firebase-messaging-sw.js] Received background message "

Can you try the following payload code:

fcm = FCM.new("FIREBASE_CLOUD_MESSAGING_KEY")

payload = {
  data: {
    object: data[:object],
    type: data[:type],
  }
}
fcm.send(DEVICE_TOKEN, payload)

Just to see if that changes the output.

@chipchipzero
Copy link
Author

@gauntface thanks for your response.
I didn't see any thing in console. I have been tried your code already but it doesn't work.

@FluorescentHallucinogen
Copy link
Contributor

See #71.

@gauntface
Copy link

@thanhlongmuah Sorry, I'm going to need more information to help solve this.

Can you provide example CURL requests you are sending?

@chipchipzero
Copy link
Author

@gauntface I found the reason: fcm ruby gem add notification automatically

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants