【问题标题】:I have problem in custom notification in android我在android中的自定义通知有问题
【发布时间】:2020-07-04 08:02:01
【问题描述】:

这是我的 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());
    }
}

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    如果您没有收到任何远程消息或您的服务无法正常工作,请查看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());      }     
    

    收到远程消息后,上述代码工作正常。如果您想添加任何自定义布局,请使用以下代码

        // Get the layouts to use in the custom notification
        RemoteViews notificationLayout = new RemoteViews(getPackageName(),           R.layout.notification_small);
         RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
    
        // Apply the layouts to the notification
         Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.notification_icon)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                .setCustomContentView(notificationLayout)
                .setCustomBigContentView(notificationLayoutExpanded)
                .build();
    

    Notification custom layout

    注意:在 android 10 及以上版本中,Notification Service 基本上不会在后台调用。您将收到通知,但不会反映自定义设计。不过,如果您还想在后台接收通知。然后收听广播接收器以获取 firebase 消息事件。那个时候你必须将你的应用唤醒到前台。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多