gpt4 book ai didi

java - Firebase 消息服务无法在旧版 Android 上运行

转载 作者:行者123 更新时间:2023-12-02 09:23:17 27 4
gpt4 key购买 nike

最近,我已将 GCM 迁移到 FCM,经过一番努力,在这个伟大社区的帮助下,我成功地使一切正常运行。

现在,当我在旧版本的 Android (Nougat) 上测试通知时,它不起作用,应用程序崩溃了,我发现它与版本有关,因为旧版本不支持 channel 管理器。

我在 StackOverflow 上找到了一些解决方案,但我还没有找到适合我的问题的解决方案,所以我希望有人能给我一个提示或解决方案。

我感谢所有的答案和帮助。

public class MessagingService extends FirebaseMessagingService {

private static final String TAG = "FCM Message";

public MessagingService() {
super();
}



@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);

String message = remoteMessage.getData().get("team");



Intent intent=new Intent(getApplicationContext(),MainActivity.class);
String CHANNEL_ID="channel";
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,"channel",NotificationManager.IMPORTANCE_HIGH);
PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0);
Notification notification=new Notification.Builder(getApplicationContext(),CHANNEL_ID)
.setContentText(message)
// .setContentTitle(title)
.setSound(defaultSound)
.setContentIntent(pendingIntent)
.setChannelId(CHANNEL_ID)
.setSmallIcon(android.R.drawable.sym_action_chat)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MAX)
.build();

NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
notificationManager.notify(1,notification);
}

}

最佳答案

我认为它正在崩溃,因为早于 Oreo 的 Android 版本不支持通知 channel 。因此,您可以通过添加 Android sdk 版本检查器并在应用程序在 Android Oreo 或更高版本上运行时设置通知 channel 来修复此问题:

    Intent intent=new Intent(getApplicationContext(),MainActivity.class);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
Notification notification=new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setContentText(message)
.setSound(defaultSound)
.setContentIntent(pendingIntent)
.setSmallIcon(android.R.drawable.sym_action_chat)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MAX)
.build();

NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String CHANNEL_ID="channel";
NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,"channel",NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(1, notification);

另请注意,我使用了NotificationCompat.Builder而不是Notification.Builder并删除了.setChannel(),因为当我们在构建器构造函数中传递 channel ID时,它是不必要的。

关于java - Firebase 消息服务无法在旧版 Android 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59808320/

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