gpt4 book ai didi

android firebase 通知不起作用

转载 作者:行者123 更新时间:2023-11-29 16:46:21 25 4
gpt4 key购买 nike

我在我的应用程序中设置了 Firebase 消息传递,但遗憾的是没有收到通知。

我正确设置了 Firebase,它已连接到我的应用程序,我还发送了一些测试消息,在 Firebase 中显示已完成,但我没有在手机上收到它们。

我的应用程序还没有在商店中,我正在通过 Android Studio 开发和测试它。

这是我的 MyFirebaseInstanceIDService 类

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG = "MyFirebaseIIDService";

/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
// [START refresh_token]
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);


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

/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}

这是 MyFirebaseMessagingService 类:

公共(public)类 MyFirebaseMessagingService 扩展 FirebaseMessagingService { private static final String TAG = "MyFirebaseMsgService";

/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// [START_EXCLUDE]
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
// [END_EXCLUDE]


//"Title","Message","NotyType", "hotelStatus"

String title = "";
if (remoteMessage.getNotification().getTitle() != null){
title = remoteMessage.getNotification().getTitle();
}

String message = "";
if (remoteMessage.getNotification().getBody() != null){
message = remoteMessage.getNotification().getBody();
}

Log.e("notification","recieved");


sendNotification(title, message);
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
// [END receive_message]


private void sendNotification(String title, String body) {
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pi = PendingIntent.getActivity(this,
0 /* Request code */,
i,
PendingIntent.FLAG_ONE_SHOT);

Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
getString(R.string.default_notification_channel_id))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(sound)
.setContentIntent(pi);

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

assert manager != null;
manager.notify(0, builder.build());
} }

调试时我没有看到任何日志,也没有在手机上收到任何通知。

我是不是做错了什么?你能请教吗?谢谢。

最佳答案

onMessageReceived 不会通过仅使用通知标记发送的推送来调用,并且您的应用程序不在前台。如果它确实在前台,您的 onMessageReceived 将被调用。

如果您希望触发 onMessageReceived,您需要发送带有附加数据标签或仅带有数据标签的推送。

但是请注意,如果您同时发送通知和数据标签,您的 onMessageReceived 只会在您的应用程序在前台时被触发,如果它在后台,数据标签内的所有内容都将作为额外内容在点击 Intent 中传递

无论您的应用是否在前台,只有数据标签会始终调用 onMessageReceived。

例如:只是一个数据标签:)

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

关于android firebase 通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47948212/

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