Push notification not working with FCM

2019-06-08 10:53发布

I am trying to get push notifications using Firebase in my Android app. The problem is when the app is in the foreground, I receive the notification and onMessageReceived() is called, however when I am on the background, I don't receive any notification and the onMessageRecieved isn't called.

What am I doing wrong?

Manifest.xml

<service android:name=".notifications.MyFirebaseMessagingService">
    <intent-filter android:priority="2147483647">
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

MyFirebaseMessagingService.class

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("MessagingService", "onCreate Firebase Service");
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.e("MessagingService", "onMessageRecieved");

        String body     = remoteMessage.getData().get("body");

        ooVooSdkSampleShowApp application = (ooVooSdkSampleShowApp) getApplication();

        Intent intent = new Intent(application.getContext(), MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(application.getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder b = new NotificationCompat.Builder(application.getContext());

        b.setAutoCancel(false)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.image_calendar_red)
                .setContentText(body)
                .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
                .setContentIntent(contentIntent)
                .setContentInfo("Info");

        NotificationManager notificationManager = (NotificationManager) application.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, b.build());

    }
}

2条回答
该账号已被封号
2楼-- · 2019-06-08 11:18

Figured it out. It was the problem with the way server response being handled by Firebase SDK. I was sending notification using "notification" field from the server side.

Notification = Just show Notification when the app is in foreground. This is the rule for Firebase SDK/ GCM SDK

Data = Show Notification when the app is in background. This is the rule for Firebase SDK/GCM SDK.

On server side, I have done it like this:

body: JSON.stringify({
            notification: {
            data: {
                 title: message
             },
             to : '/topics/user_'+username

Further Info

查看更多
戒情不戒烟
3楼-- · 2019-06-08 11:29

For receiving the notification in the background you have to add this lines to your app launcher activity. and see image for sending the data from firebase console. From the key we get the data.

   if (getIntent().getExtras() != null)
    {
        Object title = getIntent().getExtras().get("title");
        Object message = getIntent().getExtras().get("message");


        String tit=title+"";
        String msg=message+"";

        if(!tit.equals("null") && !msg.equals("null")) {
            //add your code which you want to perform on notification receive
        }

    }

enter image description here

查看更多
登录 后发表回答