当应用在后台或未运行时,推送通知无法正常工作

Olo*_*ing 30 service android push-notification android-notifications firebase-cloud-messaging

我正在使用Firebase Cloud Messaging发送推送通知.

这是我的FirebaseMessageService:

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

而且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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在以下内容中声明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>
Run Code Online (Sandbox Code Playgroud)

所以问题是当App正在运行时ticker显示良好并且通知带有默认声音,但是当App处于前台或未运行时,通知没有任何声音,ticker并且未显示在状态栏中.
为什么会发生这种情况,我该如何解决?

Tim*_*Tim 68

使用FCM,您可以指定要发送到的POST有效负载https://fcm.googleapis.com/fcm/send.在该有效负载中,您可以指定一个data或一个notification键,或两者.

如果您的有效负载仅包含data密钥,则您的应用将自行处理所有推送消息.例如,它们全部交付给您的onMessageReceived处理程序.

如果您的有效负载包含notification密钥,则只有当您的应用处于活动状态/前台时,您的应用才能自行处理推送消息.如果不是(因此它在后台,或完全关闭),FCM会通过使用您放入notification密钥有效负载的值来处理显示通知.

请注意,从控制台(如Firebase控制台)发送的通知,它们始终包含notification密钥.

看起来你不想被处理的消息FCM自己,所以你可以自定义通知等多一点,所以这将是更好地不包括notification在POST有效载荷的关键,因此,所有推送消息被传递到你的onMessageReceived.

您可以在此处阅读更多相关信息:
高级消息传递选项
下游消息语法

  • 谢谢,你是对的,当我删除“通知”键时,我开始正确接收通知 (2认同)