我已经Baidu push notifications
在我的 Android 应用程序中实现了。无论应用程序是否正在运行,推送通知在旧版本的 Android 之前都可以正常工作。但是,Android Pie (Android-9)
我只能在应用程序位于前台或后台堆栈中获得推送通知。
如果应用程序从应用程序的后台堆栈中删除,则应用程序不会收到通知。
我使用的是最新版本的百度推送 SDK,下载自: https ://push.baidu.com/sdk/push_client_sdk_for_android
我在 Android Pie 上尝试了官方百度推送 SDK 包本身中的示例。但我面临同样的问题。如果应用程序在前台或后台堆栈中,我们会收到通知,否则我们不会收到通知。
下面是我的推送通知接收器类:
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......");
}
}
在上面的代码中,onMessage(...)
如果应用程序未运行(前台或后台),则不会调用回调方法本身。
Notification Channels
PS 请注意,这与中引入的无关Oreo
,因为我已经在我的应用程序中处理了它,并且在 Push SDK 示例中也处理了同样的问题。
在 Android Oreo 和 Pie 中处理此问题的任何修复/解决方法都将非常有帮助。
谢谢你。