gpt4 book ai didi

java - Android 通知栏中不显示推送通知

转载 作者:行者123 更新时间:2023-12-02 09:39:42 25 4
gpt4 key购买 nike

我刚刚迁移到 androidx 来实现 firebase 消息传递。我已将所有内容更改为 androidx 并将我的应用程序连接到 firebase。首先,当我从 firebase 控制台发送通知时,它崩溃了。为了避免崩溃,我将 FirebaseApp.initializeApp(this); 放在我的 MainActivity 中。

但我在通知列表中仍然没有看到任何通知。

在我的 list 中,我放入了

<service android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

对于MyFirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String TAG = "FirebaseSerive";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");

Notification notification = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.ss_icon)
.build();
NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(/*notification id*/0, notification);
}
}

更新代码后,我的模拟器显示无法在 channel “null”上发布通知

感谢您提前提供的帮助。希望您能帮助我。

最佳答案

你可以尝试使用OREO中引入的这个 channel 东西。可能这就是导致问题的原因。

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

public static final String TAG = "FirebaseSerive";
public static final String NOTIFICATION_CHANNEL_ID = "IMP_NOTIFICATIONS";


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();

Notification.Builder notification = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.ss_icon);

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

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
notification.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}

mNotificationManager.notify(/*notification id*/0, notification.build());
}
}

编辑:可能您正在获取通知负载而不是数据,您可以使用下面的代码进行检查。

    // Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}

Read more here for example .

Docs

关于java - Android 通知栏中不显示推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57196893/

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