当应用程序在后台或未运行时,推送通知的工作不正确。

38
我使用Firebase云消息传递来发送推送通知。
这是我的代码:
public class FireBaseMessageService extends FirebaseMessagingService {


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e("TAG", "From: " + remoteMessage.getFrom());
    Log.e("TAG", "Notification Message Body: " + remoteMessage.getData().get("CardName")+"  :  "+remoteMessage.getData().get("CardCode"));
    sendNotification(remoteMessage.getNotification().getBody());
}


private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, StartActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher_final)
            .setContentTitle("Notification")
            .setContentText(messageBody)
            .setTicker("Test")
            .setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setContentIntent(pendingIntent);

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

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

FirebaseInstanceServer

public class FirebaseInstanceService extends FirebaseInstanceIdService {


@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.e("TAG", "Refreshed token: " + refreshedToken);

    // TODO: Implement this method to send any registration to your app's servers.
    sendRegistrationToServer(refreshedToken);

}

private void sendRegistrationToServer(String token) {


      // Add custom implementation, as needed.
        Log.e("TAG", "Refreshed token2: " + token);
    }
}

AndroidManifest中声明:

<service
    android:name=".util.notifications.FireBaseMessageService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

<service
    android:name=".util.notifications.FirebaseInstanceService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

问题是当应用程序正在运行时,ticker正常显示并带有默认声音的通知,但当应用程序在后台或未运行时,通知没有任何声音,并且状态栏中不显示ticker
为什么会发生这种情况,我该如何解决?

3个回答

85
使用 FCM,您可以指定要发送到 https://fcm.googleapis.com/fcm/send 的 POST 负载。在该负载中,您可以指定一个datanotification 键,或两者都包括。
如果您的负载只包含一个 data 键,则您的应用程序将自行处理所有推送消息。例如,它们全部被传递到您的 onMessageReceived 处理程序。
如果您的负载包含一个 notification 键,则仅当您的应用程序处于活动状态/在前台时,您的应用程序才会自己处理推送消息。如果不是这样(因此它处于后台或完全关闭),则 FCM 通过使用您放入 notification 键负载中的值来处理显示通知。
请注意,从控制台 (例如 Firebase 控制台) 发送的通知始终包括一个 notification 键。
看起来您希望自己处理 FCM 消息,以便可以更改通知等内容,因此最好不要在 POST 负载中包含 notification 键,这样所有推送消息都将传递到您的 onMessageReceived 中。
您可以在以下链接中了解更多信息:
高级消息选项
下行消息语法

2
谢谢,你是对的。当我删除 notification 键后,我开始正确地接收通知。 - Ololoking
是的,我确定。毕竟那就是它们的用途。 - Tim
@2ndGAB 在“高级消息选项”中已经很好地解释了,我也提供了链接 :-) - Tim
1
@miroslavign 我不知道这个。谢谢分享 :) - Tim
非常有用的信息!谢谢! - SuperNOVA
显示剩余10条评论

2

1
{
"to": "user_device_token",
"collapse_key": "type_a",

"data": {
    "body": "your message body",
     "title": "notification title",
    "description":"notification description ",
    "imageUrl":"image url",
    "key_1": "Value for key_1",
    "key_2": "Value for key_2"
       }
}

从后端的通知消息JSON响应中删除notification


这个 key 在哪里?user_device_token?var message = new Message() { Data = Data, Token = token, Notification = new Notification() { Body = notificationBody, Title = title } }; - Winteriscoming

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接