FCM notification message are not received in android Oreo version?

前端 未结 9 1682
轻奢々
轻奢々 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 13:59

    Then you are sending notifications without data{} object in your json. That's an old bug(?) or like supposed to work like that. If you don't have any data in your notification, then you won't trigger notifications while your app is on foreground.

    Example json:

    "notification":
    {
      "title": "notification_title",
      "body": "notification_body"
    },
    
    "data":
    {
      "example":"hey",
      "example2":"you need me."
    },
    
    "priority" : "high",
    
    "registration_ids":
    [
      "crYjxvFkASE:APA91bGv4GWj9erJ6LsblEzpag5ObkcESEsBthxsJObJ38DhZ3GbSMLlGQK3qS_qvUvrcrg_cqBgCWhBeq1X2wgxO7gmcc_gW0jM4qZYYugF5wraTHwvDKNnjQwn8dpyGEbFMXLOCvE9"
    ]
    
    0 讨论(0)
  • 2021-02-18 14:00

    "Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel or it will not appear."

    Individual notifications must now be put in a specific channel. (Reference)

    Option 1 [simple] Change the target android version Android 7.1 (API level 25) or lower.

    compileSdkVersion 25
        defaultConfig {
            applicationId "com.fcm"
            minSdkVersion 16
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    

    Option 2 If you don't want to change the target version then follow the following method

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         NotificationChannel nc = new NotificationChannel(“[enter your product id]”, “[Name]”,NotificationManager.IMPORTANCE_MAX);
         nc.setDescription(“[your description for the notification]”);
         nc.enableLights(true);
         nc.setLightColor(Color.GREEN);
         nm.createNotificationChannel(nc);
      }
    

    Use following Builder constructor

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(appContext, [id you mentioned above in constructor of NotificationChannel])
    

    Create the notification from the Builder

    nm.notify("0", notificationBuilder.build())
    
    0 讨论(0)
  • 2021-02-18 14:02

    You can use below class for generating notification in lower and upper version of android(Tested from 4.2(Jelly Bean) to 8.1.1(Oreo)).

    public final class NotificationHelper extends Thread {
    
        private Context context;
        private NotificationManager notifManager;
        private NotificationCompat.Builder notification_compact;
        private Notification.Builder notification_builder;
        private int OREO_NOTIFICATION_TYPE = 2; //setting default notificaiton type 2, as 1 is not for constant updating
        //put string channel name and id in **<>**
        private static final String CHANNEL_ONE_ID = "<Your_channel_string_ID>";
        private static final String CHANNEL_ONE_NAME = "<Your channel_String_NAME>";
        private static final String CHANNEL_TWO_ID = "<Your_channel_string_ID_TWO>";
        private static final String CHANNEL_TWO_NAME = "<Your channel_String_NAME_TWO>";
        private String title = "", message = "";
    
    
        /**
         * @param context content of activity
         * @param title   title for notification_compact
         * @param message message to show in notification_compact
         */
        public NotificationHelper(Context context, String title, String message) {
            this.context = context;
            this.title = title;
            this.message = message;
            notifManager = getManager();
        }
    
    
        @Override
        public void run() {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
                //do stuff for oreo
                createChannels();
                postNotificationAboveV25(OREO_NOTIFICATION_TYPE, title);
            } else {
                //do stuff for other versions
                postNotificationUptoV25();
            }
        }
    
    
        //method to show notificaiton above nougat
        private void postNotificationAboveV25(int id, String title) {
            notification_builder = getNotificatonBuilder(id, title);
    
            if (notification_builder != null) {
                getManager().notify(id, notification_builder.build());
            }
        }
    
    
        //get pending intent to launch activity
        private PendingIntent getPendingIntent() {
    
            Intent startActivity = context.getPackageManager()
                    .getLaunchIntentForPackage(context.getPackageName())
                    .setPackage(null)
                    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    
            return PendingIntent.getActivity(context, 0, startActivity, 0);
    
    //        Intent resultIntent = new Intent(context, ActionActivity.class);
    //        return PendingIntent.getActivity(context, AppHelper.NOTIFICATION_ID, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        }
    
    
        //method to get notification builder above nougat
        private Notification.Builder getNotificatonBuilder(int id, String title) {
            switch (id) {
                case 1:
                    return notification_builder = getNotification1(title, message);
    
                case 2:
                    return notification_builder = getNotification2(title, message);
    
                default:
                    return notification_builder = getNotification2(title, message);
            }
        }
    
    
        //Create the notification_compact that’ll be posted to Channel One
        //use this one if your notification is a type of push notificaiton
        //                              or
        //if you are not updating it in continues intervals like every 4 or 5 seconds(ex. Timer)
        @SuppressLint("NewApi")
        private Notification.Builder getNotification1(String title, String body) {
            return new Notification.Builder(context, CHANNEL_ONE_ID)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setSmallIcon(R.drawable.app_icon)
                    .setAutoCancel(true)
                    .setTicker(title + AppHelper.getMessage(R.string.started))
                    .setColor(AppHelper.getColor(context, R.color.colorPrimary))
                    .setContentIntent(getPendingIntent());
        }
    
    
        //Create the notification_compact that’ll be posted to Channel Two
        //use this for continues intervals or updating continuesly
        @SuppressLint("NewApi")
        private Notification.Builder getNotification2(String title, String body) {
            return new Notification.Builder(context, CHANNEL_TWO_ID)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setTicker(title + AppHelper.getMessage(R.string.started))
                    .setSmallIcon(R.drawable.app_icon)
                    .setAutoCancel(true)
                    .setColor(AppHelper.getColor(context, R.color.colorPrimary))
                    .setContentIntent(getPendingIntent());
        }
    
    
        //method to post notification upto Nougat i.e., below api level 26
        @SuppressLint("NewApi")
        private void postNotificationUptoV25() {
            notification_compact = new NotificationCompat.Builder(context);
            notification_compact.setAutoCancel(true);
            notification_compact.setSmallIcon(R.drawable.app_icon);
            notification_compact.setTicker(title + AppHelper.getMessage(R.string.started));
            notification_compact.setContentTitle(title);
            notification_compact.setContentText(message);
            notification_compact.setColor(AppHelper.getColor(context, R.color.colorPrimary));
            notification_compact.setContentIntent(getPendingIntent());
    //        notification_compact.setWhen(1506067106762L);
    
            getManager().notify(AppHelper.NOTIFICATION_ID, notification_compact.build());
        }
    
    
        //method to update notification
        public void updateNotification(String time) {
    
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
                //update above NOUGAT V25
                if (notification_builder != null) {
                    notification_builder.setContentText(message + "  " + time);
                    getManager().notify(AppHelper.NOTIFICATION_ID, notification_builder.build());
                }
            } else {
                //update below NOUGAT V25
                if (notification_compact != null) {
                    notification_compact.setContentText(message + "  " + time);
                    getManager().notify(AppHelper.NOTIFICATION_ID, notification_compact.build());
                }
            }
        }
    
        //method to update remainting notification
        public void updateRemainingNotification(String time) {
    
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
                //update above NOUGAT V25
                if (notification_builder != null) {
                    notification_builder.setContentText(time + AppHelper.getMessage(R.string.remaining));
                    getManager().notify(AppHelper.NOTIFICATION_ID, notification_builder.build());
                }
            } else {
                //update below NOUGAT V25
                if (notification_compact != null) {
                    notification_compact.setContentText(time + AppHelper.getMessage(R.string.remaining));
                    getManager().notify(AppHelper.NOTIFICATION_ID, notification_compact.build());
                }
            }
        }
    
    
        //method to create channels which is necessary above Nougat(API - 25) i.e., at Oreo(API - 26)
        @SuppressLint("NewApi")
        private void createChannels() {
    
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
                    CHANNEL_ONE_NAME, notifManager.IMPORTANCE_DEFAULT);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            getManager().createNotificationChannel(notificationChannel);
    
            NotificationChannel notificationChannel2 = new NotificationChannel(CHANNEL_TWO_ID,
                    CHANNEL_TWO_NAME, notifManager.IMPORTANCE_DEFAULT);
            notificationChannel2.enableLights(false);
            notificationChannel2.enableVibration(true);
            notificationChannel2.setLightColor(Color.RED);
            notificationChannel2.setShowBadge(false);
            getManager().createNotificationChannel(notificationChannel2);
    
        }
    
    
        //method to get Object of Notification Manager
        private NotificationManager getManager() {
            if (notifManager == null)
                notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
            return notifManager;
        }
    
    
        /**
         * call this method to destroy notification
         */
        public void destroyNotification() {
            if (notifManager != null)
                notifManager.cancel(AppHelper.NOTIFICATION_ID);
        }
    }
    

    Just call this class from FCM, with context and msg. As its a thread class you can update the notification constantly also.

    Don't forget to call destroyNotification() method, when your work ends.

    You can discover and make changes in it, as you like

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

    You need to to ad permission and to create a notification channel

    public static void createChannelAndHandleNotifications(Context context) {
            ctx = context;
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(
                        NOTIFICATION_CHANNEL_ID,
                        NOTIFICATION_CHANNEL_NAME,
                        NotificationManager.IMPORTANCE_HIGH);
                channel.setDescription(NOTIFICATION_CHANNEL_DESCRIPTION);
                channel.setShowBadge(true);
    
                NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }
        }
    
    0 讨论(0)
  • 2021-02-18 14:05
    private void generateNotification(String message, String title) {
    
        Intent intent = new Intent(getApplicationContext(), ActitivtyNotification.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.stkfood_logo)  //a resource for your custom small icon
                .setContentTitle(title) //the "title" value you sent in your notification
                .setContentText(message) //ditto
                .setAutoCancel(true)  //dismisses the notification on click
                .setSound(defaultSoundUri);
    
        //Setting up Notification channels for android O and above
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel("3", "CHANNEL_NAME", importance);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            assert mNotificationManager != null;
            notificationBuilder.setChannelId("3");
            mNotificationManager.createNotificationChannel(notificationChannel);
        }
        PendingIntent contentIntent = PendingIntent.getActivity(this, 3, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(3, notificationBuilder.build());
    }
    
    0 讨论(0)
  • 2021-02-18 14:06
      notification = new NotificationCompat.Builder(this, ANDROID_CHANNEL_ID)
                        .setSmallIcon(R.drawable.ic_small_logo)
                        .setLargeIcon(picture)
                        .setContentTitle("Title")
                        .setContentText("Body")
                        .setContentIntent(pendingIntent)
                        .setAutoCancel(true)
                        .build();
    
    • ANDROID_CHANNEL_ID = "CHANNEL_ID"
    • PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    • intent = new Intent(getApplicationContext(), HomeActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    0 讨论(0)
提交回复
热议问题