【问题标题】:Push notification not working with FCM推送通知不适用于 FCM
【发布时间】:2017-02-26 19:06:19
【问题描述】:

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

我做错了什么?

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

    }
}

【问题讨论】:

标签: android firebase notifications firebase-cloud-messaging


【解决方案1】:

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

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

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

在服务器端,我是这样做的:

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

Further Info

【讨论】:

    【解决方案2】:

    为了在后台接收通知,您必须将此行添加到您的应用启动器活动中。 并查看从 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
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-31
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      相关资源
      最近更新 更多