Firebase通知不在后台工作 [英] Firebase Notification not working in background

查看:171
本文介绍了Firebase通知不在后台工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助。 Firebase通知不在后台工作。这是我的代码:

  @Override 
public void onMessageReceived(RemoteMessage remoteMessage){

Log.d(TAG,FROM:+ remoteMessage.getFrom());
sharedPreference = getSharedPreferences(Global.SECURETRADE,0);
UID = sharedPreference.getString(Global.ID,);


Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)`在此输入代码`;

NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this);

if(android.os.Build.VERSION.SDK_INT> = Build.VERSION_CODES.LOLLIPOP){

notificationBuilder.setSmallIcon(R.mipmap.small_secure_trade_app_icon);

} else {

notificationBuilder.setSmallIcon(R.drawable.small_secure_trade_app_icon);
}

notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.drawable.securetrade_icon));
notificationBuilder.setContentTitle(remoteMessage.getData()。get(title));
notificationBuilder.setContentText(remoteMessage.getData()。get(body));
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0,notificationBuilder.build());






$ / code $ / pre $ b您是否正在发送数据消息(而不是通知消息)?


$ b

通知消息不要调用 onMessageReceived()


当您希望FCM处理代表客户端应用程序显示
通知时使用通知消息。当您
想要处理客户端应用程序中的消息时使用数据消息。

阅读更多: https://firebase.google.com/docs/cloud-messaging/concept-options#通知和数据消息



PS:FCM Web控制台始终发送通知消息




如果您正在发送数据消息,并且 onMessageReceived()未被调用...

则是一个不同的问题。

它甚至可能是特定设备的问题。

请参阅


I need help. Firebase Notifications is Not Working in Background. This is My Code:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "FROM:" + remoteMessage.getFrom());
   sharedPreference = getSharedPreferences(Global.SECURETRADE, 0);
    UID = sharedPreference.getString(Global.ID, "");


        Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)`enter code here`;

        NotificationCompat.Builder notificationBuilder = new
                NotificationCompat.Builder(this);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            notificationBuilder.setSmallIcon(R.mipmap.small_secure_trade_app_icon);

        } else {

            notificationBuilder.setSmallIcon(R.drawable.small_secure_trade_app_icon);
        }

        notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.securetrade_icon));
        notificationBuilder.setContentTitle(remoteMessage.getData().get("title"));
        notificationBuilder.setContentText(remoteMessage.getData().get("body"));
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSound(defaultSoundUri);
        notificationBuilder.setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager)
                        getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());

    }




}

解决方案

Are you sending data-messages (not notification-messages) ?

notification-messages don't call onMessageReceived()

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

PS: FCM Web Console always sends notification-messages


If you are sending a data-messages, and onMessageReceived() is not called...
then it's a different problem.

It might even be a problem of that specific device.
See Push notifications using FCM not received when app is killed android

这篇关于Firebase通知不在后台工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆