Problem description
After you integrate the push SDK, devices running Android 8.0 or later cannot receive push messages. Logs indicate that the server sent the notification to the client, but the client did not create the notification.
Cause
Android 8.0 (API Level 26) introduced notification channels to classify and manage notifications. If your app's targetSdkVersion is 26 or higher and you do not set a NotificationChannel, the notifications that you create will not be displayed.
Solution
Note:
Before you perform risky operations, such as modifying instances or data, ensure that your instances have disaster recovery and fault tolerance capabilities to maintain data security.
Before you modify the configurations or data of instances, such as ECS and RDS instances, create snapshots or enable features such as RDS log backup.
If you have submitted security information, such as logon credentials, to Alibaba Cloud, change them promptly.
Alibaba Cloud Mobile Push supports notification channels in version 3.1.1 and later. Follow these steps to locate and resolve the issue:
Integrate the latest SDK
Integrate Mobile Push SDK V3.1.1 or later.
Integrate the server-side OpenAPI SDK V3.9.0 or later:
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-push</artifactId> <version>3.9.0</version> </dependency>
Register a NotificationChannel
Use the following code to create a NotificationChannel on the client. Call this code in the `onCreate` method of your application. You can register the channel before or after you initialize Alibaba Cloud Mobile Push. For more information, see the Demo.
Note: After you modify a NotificationChannel, uninstall and reinstall the app for the changes to take effect.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The ID of the notification channel.
String id = "1";
// The name of the notification channel that is visible to users.
CharSequence name = "notification channel";
// The description of the notification channel that is visible to users.
String description = "notification description";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
// Configure the properties of the notification channel.
mChannel.setDescription(description);
// Set the notification light, if the Android device supports it.
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
// Enable vibration for notifications, if the Android device supports it.
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
// Create the notification channel in the notification manager.
mNotificationManager.createNotificationChannel(mChannel);
}
Push notifications
Push notifications using OpenAPI
When you send a push notification from the server, specify the ID of the notification channel. See the following code for details:
@Test
public void testAdvancedPush() throws Exception {
PushRequest pushRequest = new PushRequest();
// The push target.
pushRequest.setAppKey(appKey);
pushRequest.setTarget("DEVICE"); // The push target. DEVICE: push to a device. ACCOUNT: push to a specified account. TAG: push to a custom tag. ALL: push to all.
pushRequest.setTargetValue("xxxxxxxxxxxxxxx");
pushRequest.setPushType("NOTICE"); // The message type. Valid values: MESSAGE and NOTICE.
pushRequest.setDeviceType("ANDROID"); // The device type. Valid values: ANDROID, iOS, and ALL.
// The push configuration.
pushRequest.setTitle("ALi Push Title"); // The message title.
pushRequest.setBody("Ali Push Body"); // The message body.
// Push configuration for Android.
pushRequest.setAndroidNotifyType("BOTH");// The notification method. VIBRATE: vibration. SOUND: sound. BOTH: sound and vibration. NONE: silent.
pushRequest.setAndroidOpenType("APPLICATION"); // The action to perform after the notification is tapped. APPLICATION: open the application. ACTIVITY: open an Android Activity. URL: open a URL. NONE: no action.
// Specify the notification channel ID.
pushRequest.setAndroidNotificationChannel("1");
......
}
Push notifications using the Alibaba Cloud console
Log on to the EMAS console and select the Mobile Push product.
On the Mobile Push page, in the navigation pane on the left, choose Create Task > Push Notification.
Enter the Notification ID. This ID must match the ID of the NotificationChannel that you registered on the client. Then, click Push Notification.
Notes on NotificationChannel
After you specify a NotificationChannel, notification methods, such as vibration or sound, are determined by the NotificationChannel settings. The server-side notification method settings no longer take effect.
Notification permissions for a NotificationChannel are granted by default on devices from most manufacturers. However, on OPPO and VIVO devices, you must manually enable these permissions.
Applies to
Mobile Push