FCM notification message are not received in android Oreo version?

前端 未结 9 1688
轻奢々
轻奢々 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: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())
    

提交回复
热议问题