我在安卓的自定义通知中遇到了问题。
我在安卓的自定义通知中遇到了问题。
这是我的Firebase消息服务代码文件。我的服务没有被调用。
public class NotificationService extends FirebaseMessagingService { String myTitle, myImage; @Override public void onMessageReceived(RemoteMessage remoteMessage) { myTitle = remoteMessage.getData().get("title"); myImage = remoteMessage.getData().get("body"); Log.d("Service","Hi this is service"); //bitmap = getBitmapfromUrl(myImage); //Toast.makeText(this,"Hi test",Toast.LENGTH_SHORT).show(); showNotification(myTitle,myImage); } private void showNotification(String myTitle,String myImage) { Intent intent = new Intent(this, MainActivity.class); intent.putExtra("Notification_Title", "yes"); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //Toast.makeText(this,title,Toast.LENGTH_SHORT).show(); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); String channelId = "Custom_Notification"; Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ic_notifications_none_white_24dp) .setContentTitle(myTitle) .setContentText(myImage) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(channelId, "Custom Notification", NotificationManager.IMPORTANCE_HIGH); notificationManager.createNotificationChannel(channel); } notificationManager.notify(0, notificationBuilder.build()); } }
在安卓中,我在自定义通知方面遇到了问题。如果你没有收到任何远程消息或者你的服务不起作用,可以查看StackOverflow链接。
要创建自定义通知频道,可以使用以下代码:
private void showNotification(String myTitle, String myImage) { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("Notification_Title", "yes"); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); String channelId = "Custom_Notification"; Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ic_notifications_none_white_24dp) .setContentTitle(myTitle) .setContentText(myImage) .setSound(defaultSoundUri) .setContentIntent(pendingIntent) .setDefaults(NotificationCompat.DEFAULT_ALL) .setAutoCancel(true) .setPriority(NotificationCompat.PRIORITY_HIGH); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(channelId, "Custom Notification", NotificationManager.IMPORTANCE_HIGH); notificationManager.createNotificationChannel(channel); } notificationManager.notify(0, notificationBuilder.build()); }
上述代码在接收到远程消息后可以正常工作。如果想要添加自定义布局,则可以使用以下代码:
// 获取自定义通知的布局 RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small); RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large); // 将布局应用到通知中 Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setStyle(new NotificationCompat.DecoratedCustomViewStyle()) .setCustomContentView(notificationLayout) .setCustomBigContentView(notificationLayoutExpanded) .build();
自定义通知布局的更多信息可以参考这里。
注意:在安卓10及以上版本中,通知服务在后台不会被调用。你会收到通知,但是自定义的设计不会生效。如果你想要在后台接收通知,你需要监听Firebase消息事件的广播接收器,并将应用程序唤醒到前台。