I have implemented Baidu push notifications
in my Android app. The push notifications were working fine until older versions of Android, irrespective of whether the app is running or not. However, in Android Pie (Android-9)
i'm able to get push notifications only in the app is in Foreground or in the background stack.
If the app is removed from the background stack of applications then the app doesn't receive the notifications.
I'm using the latest version of Baidu Push SDK, downloaded from: https://push.baidu.com/sdk/push_client_sdk_for_android
I tried the sample present in the official Baidu Push SDK package itself on Android Pie. But i'm facing the same issue. If the app is in Foreground or background stack, we receive the notification, otherwise we do not receive the notification.
Below is my Push Notification receiver class:
public class BaiduPushReceiver extends PushMessageReceiver {
Context mContext;
public static final int NOTIFICATION_ID = 1;
NotificationCompat.Builder builder;
private NotificationManager mNotificationManager;
@Override
public void onBind(Context context, int errorCode, String appid,
String userId, String channelId, String requestId){
Log.d("MY_APP","----*-*-*-*-*-* ChannelID:"+channelId+"*-*-*-*-*-------");
Log.d("MY_APP","ErrorCode:"+errorCode+" AppId:"+appid+" UserId:"+userId+" ChannelId:"+channelId+" RequestId:"+requestId);
SharedPreferencesManager.storeValue(SharedPreferencesManager.DEVICE_TOKEN, channelId);
}
private void sendNotification(String msgContent, Context context)
{
Bundle extra = new Bundle();
extra.putString("message_data",msgContent);
String message="";
String type;
String id;
try{
JSONObject jsob = new JSONObject(msgContent);
String description = jsob.getString("description");
jsob = new JSONObject(description);
message = jsob.getString("message");
type = jsob.getString("type");
id = jsob.getString("id");
Log.d("MY_APP","Message:"+message);
Log.d("MY_APP","Type:"+type);
Log.d("MY_APP","Id:"+id);
}
catch (Exception e)
{
e.printStackTrace();
}
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent;
if(SharedPreferencesManager.isEmpty(SharedPreferencesManager.TOKEN))
intent = new Intent(context, poiuy.class);
else
intent = new Intent(context, asdf.class);
intent.putExtras(extra);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
message = java.net.URLDecoder.decode(message, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//contentIntent.put("NotificationMessage", notificationMessage);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel("MY_APP_CHANNEL_1", "my app Channel", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setVibrationPattern(new long[]{500, 500, 500, 500});
mNotificationManager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,"MY_APP_CHANNEL_1")
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(context.getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setVibrate(new long[]{500, 500, 500, 500})
.setSound(alarmSound)
.setAutoCancel(true)
.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
else
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(context.getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setVibrate(new long[]{500, 500, 500, 500})
.setSound(alarmSound)
.setAutoCancel(true)
.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
@Override
public void onUnbind(Context context, int i, String s) {
}
@Override
public void onSetTags(Context context, int i, List<String> list, List<String> list1, String s) {
}
@Override
public void onDelTags(Context context, int i, List<String> list, List<String> list1, String s) {
}
@Override
public void onListTags(Context context, int i, List<String> list, String s) {
}
@Override
public void onMessage(Context context, String s, String s1) {
Log.d("MY_APP","OnMessage Called......"+"\nS="+s+"\ns1="+s1);
sendNotification(s,context);
}
@Override
public void onNotificationClicked(Context context, String s, String s1, String s2) {
Log.d("BAIDU_PUSH","OnNotificationClicked called......");
}
@Override
public void onNotificationArrived(Context context, String s, String s1, String s2) {
Log.d("BAIDU_PUSH","onNotificationArrived called......");
}
}
In the above code, The onMessage(...)
callback method itself is not getting called if the app is not running (foreground or background).
P.S. please note that this is not related to the Notification Channels
which was introduced in Oreo
, as i have already handled that in my app and the same is handled in the Push SDK sample as well.
Any fix/workaround to handle this issue in Android Oreo and Pie will be really helpful.
Thank you.