gpt4 book ai didi

android - 通知不显示 firebase Android 8

转载 作者:行者123 更新时间:2023-11-29 18:53:15 25 4
gpt4 key购买 nike

我有一个应用程序应该显示推送通知,但它没有这行 new NotificationCompat.Builder(this); 在代码中被取消了。我是新手,这怎么行 这是 FirebaseMessagingService.java 类中的代码

    @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);

sendNotification(remoteMessage.getNotification().getBody());
}

private void sendNotification(String MessageBody){
Intent intent= new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher_background);
notificationBuilder.setContentTitle("Hope");
notificationBuilder.setContentText(MessageBody);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());

}

}

最佳答案

这似乎是here的重复问题.

Android O(这意味着您的targetSdk 为26 或以上),没有notification channel 将不会显示通知。 .

这是我的工作代码:)

Context c = getApplicationContext();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

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

// The id of the channel.
final String CHANNEL_ID = "default";
// The user-visible name of the channel.
final String CHANNEL_NAME = "Default";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel defaultChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(defaultChannel);
}

intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(c, CHANNEL_ID)
.setLargeIcon(your_custom_bitmap)
.setSmallIcon(R.drawable.ic_notification)
.setColor(ContextCompat.getColor(c, R.color.notification))
.setContentTitle(getString(R.string.app_name))
.setContentText(message)
.setWhen(System.currentTimeMillis())
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setLights(ContextCompat.getColor(c, R.color.colorAccentLight), 5000, 5000)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent);

notificationManager.notify("myapp", 0, notificationBuilder.build());

关于android - 通知不显示 firebase Android 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50463166/

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