1

I'm trying to push notifications to the end-users of the app when post is uploaded. It works fine when the app is in foreground but doesn't show up when then the app is in background or killed. Is there any way to show the notifications when the app is killed or running in the background. Here is the node.js code which i'm using

const functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendPush = functions.database.ref('/promos').onWrite(event => {
var topic = "deals_notification";
let projectStateChanged = false;
let projectCreated = false;
let projectData = event.data.val();

if (((event.data.numChildren())-(event.data.previous.numChildren()))>0) {
    let msg="notification arrived"
  let payload = {
        notification: {
            title: 'Firebase Notification',
            body: msg,
            sound: 'default',
            badge: '1'
        }
    };

admin.messaging().sendToTopic(topic, payload).then(function(response) {
// See the MessagingTopicResponse reference documentation for the
// contents of response.
console.log("Successfully sent message:", response);
}).catch(function(error) {
console.log("Error sending message:", error);
});
}
});

and here is the MyFirebaseMessageService:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessageService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.e("notification---",remoteMessage.getNotification().getBody());

    sendnotification(remoteMessage.getNotification().getBody());
}
private void sendnotification(String body){
    Intent intent = new Intent(this, MainActivity.class);
// use System.currentTimeMillis() to have a unique ID for the pending intent
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) 
System.currentTimeMillis(), intent, 0);

// build notification
// the addAction re-use the same intent to keep the example short
    Notification.Builder n  = new Notification.Builder(this)
            .setContentTitle("Best Deals")
            .setContentText(body)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pIntent)
            .setAutoCancel(true);

    NotificationManager manager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, n.build());
}
}

Thanks.

AL.
  • 36,815
  • 10
  • 142
  • 281

2 Answers2

1

if the problem is that onMessageReceived() is called only when the app is in foreground, and when it's in background a notification is displayed but your method is not called... than that's working correctly. See the documentation:

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.

Read more here: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

You are sending a notification-message. instead you should send a data-message

If the problem is different: when the app is in background nothing happen, it might be a problem of your device. See Push notifications using FCM not received when app is killed android

Diego Giorgini
  • 12,489
  • 1
  • 47
  • 50
1

I am sure the code is perfect. But there might be some problem with the device you are testing.Please try to test the code with some other device.

maneesh
  • 216
  • 3
  • 14