推送通知不适用于FCM

时间:2016-10-17 17:37:40

标签: android firebase notifications firebase-cloud-messaging

我正在尝试在我的Android应用中使用Firebase获取推送通知。问题是当应用程序在前台时,我收到通知并且调用了onMessageReceived(),但是当我在后台时,我没有收到任何通知,onMessageRecieved isn&# 39; t叫。

我做错了什么?

的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 个答案:

答案 0 :(得分:1)

想出来。这是Firebase SDK处理服务器响应的方式的问题。我正在使用服务器端的“通知”字段发送通知。

通知 =只需在应用处于前台时显示通知。这是Firebase SDK / GCM SDK的规则

数据 =当应用在后台时显示通知。这是Firebase SDK / GCM SDK的规则。

在服务器端,我这样做了:

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

Further Info

答案 1 :(得分:0)

要在后台接收通知,您必须将此行添加到您的应用启动器活动中。 并查看用于从firebase控制台发送数据的图像。从密钥中我们得到了数据。

   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

相关问题