FCM notification message are not received in android Oreo version?

前端 未结 9 1685
轻奢々
轻奢々 2021-02-18 13:31

I have send FCM notification from server to users. It works fine(until api 25) but in Oreo when the application have not in background(services are closed) (or) completely close

相关标签:
9条回答
  • 2021-02-18 14:08

    In oreo version can not add a notification without Channel so need to add following code in firebase notification Service class for oreo notification without channel can not send notification :

    private void sendMyNotification(String message,String title) {
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant")
            NotificationChannel notificationChannel=new NotificationChannel("my_notification","n_channel",NotificationManager.IMPORTANCE_MAX);
            notificationChannel.setDescription("description");
            notificationChannel.setName("Channel Name");
            notificationManager.createNotificationChannel(notificationChannel);
        }
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.listlogo)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.tlogo))
                    .setContentTitle(title)
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setSound(soundUri)
                    .setContentIntent(pendingIntent)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setPriority(NotificationManager.IMPORTANCE_MAX)
                    .setOnlyAlertOnce(true)
                    .setChannelId("my_notification")
                    .setColor(Color.parseColor("#3F5996"));
            //.setProgress(100,50,false);
            notificationManager.notify(0, notificationBuilder.build());
    }
    
    0 讨论(0)
  • 2021-02-18 14:09

    When you target Android 8.0 (API level 26), you must implement one or more notification channels. If your targetSdkVersion is set to 25 or lower, when your app runs on Android 8.0 (API level 26) or higher, it behaves the same as it would on devices running Android 7.1 (API level 25) or lower.

    Note: If you target Android 8.0 (API level 26) and post a notification without specifying a notification channel, the notification does not appear and the system logs an error.

    Note: You can turn on a new setting in Android 8.0 (API level 26) to display an on-screen warning that appears as a toast when an app targeting Android 8.0 (API level 26) attempts to post without a notification channel. To turn on the setting for a development device running Android 8.0 (API level 26), navigate to Settings > Developer options and enable Show notification channel warnings.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
       NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
       String id = "id_product";
       // The user-visible name of the channel.
       CharSequence name = "Product";
       // The user-visible description of the channel.
       String description = "Notifications regarding our products";
       int importance = NotificationManager.IMPORTANCE_MAX;
       NotificationChannel mChannel = new NotificationChannel(id, name, importance);
       // Configure the notification channel.
       mChannel.setDescription(description);
       mChannel.enableLights(true);
       // Sets the notification light color for notifications posted to this
       // channel, if the device supports this feature.
       mChannel.setLightColor(Color.RED);
       notificationManager.createNotificationChannel(mChannel);
    }
    

    Creating a Push Notification on Android Oreo

    To create a notification, you will use the NotificationCompat.Builder class. The constructor which was used before took only Context as a parameter, but in Android O, the constructor looks like this –

    NotificationCompat.Builder(Context context, String channelId)
    

    The following code snippet will show you how to create a notification –

    Intent intent1 = new Intent(getApplicationContext(), Ma
    
    inActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 123, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(),"id_product")
           .setSmallIcon(R.drawable.flatpnicon) //your app icon
           .setBadgeIconType(R.drawable.flatpnicon) //your app icon
           .setChannelId(id)
           .setContentTitle(extras.get("nt").toString())
           .setAutoCancel(true).setContentIntent(pendingIntent)
           .setNumber(1)
           .setColor(255)
           .setContentText(extras.get("nm").toString())
           .setWhen(System.currentTimeMillis());
    notificationManager.notify(1, notificationBuilder.build());
    

    Android O gives you a few more functions to customize your notification –

    setNumber() – allows you to set the number displayed in the long-press menu setChannelId() – allows you set the Channel Id explicitly if you are using the old constructor setColor() – allows a RGB value to put a color theme for your notification setBadgeIconType() – allows you to set an icon to be displayed in the long-press menu

    for more info check example here

    0 讨论(0)
  • 2021-02-18 14:16

    only data notifications is handled on android Oreo

    try to remove the notification

    `
     remove this key notification 
     {"notification":
     {
      "title": "notification_title",
      "body": "notification_body"
     },
     // keep only the data key 
      "data":
     {
      "example":"hey",
      "example2":"you need me."
     },
    
     "priority" : "high",
    
     "registration_ids":
        []
    
    
    `
    

    when the app is in background .. the onRecievedMessage will not called if the payload is contains both notification key and data key ..

    so remove the notification .. and only keep the data key .. and it will work well

    0 讨论(0)
提交回复
热议问题