1

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.

Zax
  • 2,870
  • 7
  • 52
  • 76
  • Hi Zax, have you figured out any fix or workaround for handling this issue? – Sparkzz Jan 21 '20 at 10:20
  • I guess this occurs due to some background optimizations that got introduced from Android O. – Sparkzz Jan 21 '20 at 10:27
  • @Sparkzz: The only workaround i could do is run a Foreground service by associating it with a notification. Also on Phone boot you need to srart this service. This ensures that a part of the application is always running and you will be able to receive the notifications. I know this is a really bad solution, but i couldn't find any other way. If you find one then please add it as the answer. – Zax Jan 21 '20 at 11:47

1 Answers1

1

I also faced the same issue while using Baidu Push SDK. As there are battery optimizations enforced on the apps since Android O, many of the devices do not receive the messages from Baidu in the terminated state. It started working in the terminated state when I revoked all battery optimization restrictions on the app.

I recommend you to use FCM instead of Baidu for push notifications. Its performance is always good than Baidu's.

  • Yes, FCM do not work there but you can try other venders like Xiaomi Push and Huawei Push. I have been using Xiaomi Push Services, it worked better than Baidu. But like Baidu, notifications were not working in terminated states for some of the devices. – Mayuresh Karekar Nov 02 '20 at 12:13