[JAVA] Android O (API version 26) push notifications

*

From Android O (API version 26), push notification "notification channel" registration is required. In other words, the target is when targetSdkVersion is 26 or more.

What is a notification channel?

The "notification channel" is equipped with a function that allows you to turn on / off notifications according to the content of the notification, such as separating "profit information" and "point deadline notification" or by news category.

Even if I raise the targetSdkVersion from 25 or less to 26 or more, Android Studio does not display any warnings. It is not done, but when I build and notify, an error is displayed as "No channel" as shown below.

E/NotificationService: No Channel found for pkg=com.example.app, channelId=null, id=1234567890, tag=null, opPkg=com.example.app, callingUid=12345, userId=0, incomingUserId=0, notificationUid=12345, notification=Notification(channel=null pri=0 contentView=null vibrate=null sound=content://settings/system/notification_sound defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)

Also, as a matter of course, the notification settings cannot be rewritten from the app side, so it is not possible for the app side to synchronize the settings before Android O with the setting screen on the app side.

Subscribe to notification channel

Register the channel as follows.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationChannel channel = new NotificationChannel(
        //Unique channel ID
        //It seems good to keep it constant here somewhere
        "channel_id_sample",

        //Channel name displayed in the settings
        //It seems good to actually specify the resource here
        "Push Notification",

        //Channel importance
        //The display location differs depending on the importance
        NotificationManager.IMPORTANCE_DEFAULT
    );

    //Enable lights on notifications
    channel.enableLights(true);
    //Light color at the time of notification
    channel.setLightColor(Color.WHITE);
    //Display level on the lock screen
    channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

    //Subscribe to the channel
    manager.createNotificationChannel(channel);
}

Grouping is also possible using NotificationChannelGroup and setGroup.

Notify notification channel

There are few changes here, just specify the channel with setChannelId.

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

Resources res = getResources();
int icon = res.getIdentifier("app_icon", "drawable", this.getPackageName());
int titleId = res.getIdentifier("app_name", "string", this.getPackageName());
String applicationName = this.getString(titleId);
String message = "Something test";

Notification.Builder mBuilder = new Notification.Builder(this)
        .setSmallIcon(icon)
        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), icon))
        .setContentTitle(applicationName)
        .setContentText(message)
        .setContentIntent(pendingIntent);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    mBuilder.setChannelId("channel_id_sample");
}

Why is it talking about Android O push notifications now?

After August of this year, new apps will not be able to apply to the Play Store unless the targetSdkVersion is raised to 26 or higher. After November of this year, existing apps will not be able to apply to the Play Store unless the targetSdkVersion is raised to 26 or higher. Following that, after 2019, it will be necessary to raise the targetSdkVersion within one year of the release of the new version.

reference

Recommended Posts

Android O (API version 26) push notifications
Compatible with Android 10 (API 29)