gpt4 book ai didi

android - 当应用程序处于后台或未运行时,推送通知无法正常工作

转载 作者:IT老高 更新时间:2023-10-28 21:50:41 26 4
gpt4 key购买 nike

我正在使用 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());
}
}

还有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 不显示在状态栏中。
为什么会发生这种情况,我该如何解决?

最佳答案

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

如果您的有效负载仅包含 data 键,您的应用将自行处理所有推送消息。例如。它们都被传递到您的 onMessageReceived 处理程序。

如果您的有效负载包含 notification 键,则您的应用会在您的应用处于 Activity 状态/在前台运行时自行处理推送消息。如果不是(所以它在后台,或完全关闭),FCM 会使用您放入 notification 键有效负载的值来处理为您显示通知。

请注意,从控制台(如 Firebase 控制台)发送的通知总是包含 notification 键。

看起来您想自己处理 FCM 消息,以便您可以自定义更多通知等,因此最好不要在 POST 有效负载中包含 notification 键,所以所有推送消息被传递到您的 onMessageReceived

您可以在此处阅读更多信息:
Advanced messaging options
Downstream message syntax

关于android - 当应用程序处于后台或未运行时,推送通知无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37876257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com