Push notification not working with FCM

允我心安 提交于 2019-12-23 02:50:57

问题


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());

    }
}

回答1:


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




回答2:


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
        }

    }



来源:https://stackoverflow.com/questions/40092465/push-notification-not-working-with-fcm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!