Android10收不到友盟推送

  • 作者:彭老师
  • 日期:2020-06-05
  • 类型:Android
  • 说明:本文源于彭老师手写摘要,如需转载请带上链接或注明出处!

维护一个3年前的项目,集成了友盟推送

官方代码如下:

package com.umeng.soexample.push;

import java.util.Random;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import com.umeng.message.UTrack;
import com.umeng.message.entity.UMessage;
import com.umeng.soexample.R;
import org.json.JSONException;
import org.json.JSONObject;

public class MyNotificationService extends Service {
private static final String TAG = UmengNotificationService.class.getName();
public static UMessage oldMessage = null;

@Override
public void onCreate() {
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null) {
return super.onStartCommand(intent, flags, startId);
}
String message = intent.getStringExtra("UmengMsg");
try {
UMessage msg = new UMessage(new JSONObject(message));
if (oldMessage != null) {
UTrack.getInstance(getApplicationContext()).setClearPrevMessage(true);
UTrack.getInstance(getApplicationContext()).trackMsgDismissed(oldMessage);
}
showNotification(msg);
} catch (JSONException e) {
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}

private void showNotification(UMessage msg) {
int id = new Random(System.nanoTime()).nextInt();
oldMessage = msg;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancelAll();
Notification.Builder mBuilder = new Notification.Builder(this);
mBuilder.setContentTitle(msg.title)
.setContentText(msg.text)
.setTicker(msg.ticker)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.umeng_push_notification_default_small_icon)
.setAutoCancel(true);
Notification notification = mBuilder.getNotification();
PendingIntent clickPendingIntent = getClickPendingIntent(this, msg);
PendingIntent dismissPendingIntent = getDismissPendingIntent(this, msg);
notification.deleteIntent = dismissPendingIntent;
notification.contentIntent = clickPendingIntent;
manager.notify(id, notification);
}

public PendingIntent getClickPendingIntent(Context context, UMessage msg) {
Intent clickIntent = new Intent();
clickIntent.setClass(context, NotificationBroadcast.class);
clickIntent.putExtra(NotificationBroadcast.EXTRA_KEY_MSG,
msg.getRaw().toString());
clickIntent.putExtra(NotificationBroadcast.EXTRA_KEY_ACTION,
NotificationBroadcast.ACTION_CLICK);
PendingIntent clickPendingIntent = PendingIntent.getBroadcast(context,
(int) (System.currentTimeMillis()),
clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);

return clickPendingIntent;
}

public PendingIntent getDismissPendingIntent(Context context, UMessage msg) {
Intent deleteIntent = new Intent();
deleteIntent.setClass(context, NotificationBroadcast.class);
deleteIntent.putExtra(NotificationBroadcast.EXTRA_KEY_MSG,
msg.getRaw().toString());
deleteIntent.putExtra(
NotificationBroadcast.EXTRA_KEY_ACTION,
NotificationBroadcast.ACTION_DISMISS);
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(context,
(int) (System.currentTimeMillis() + 1),
deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT);
return deletePendingIntent;
}

@Override
public IBinder onBind(Intent intent) {
return null;
}
}

问题:<=7.0版本以下没问题,>8版本Debug时能进入UmengNotificationService代码。却无法显示在手机通知栏。于是找了台安卓10版本亲测改了下代码,希望能帮到大家!

package com.xxx.yyy.push;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.provider.MediaStore;

import com.xxx.yyy.R;
import com.umeng.message.UTrack;
import com.umeng.message.entity.UMessage;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Random;

public class MyNotificationService extends Service {

public static UMessage oldMessage = null;

@Override
public void onCreate() {
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null) {
return super.onStartCommand(null, flags, startId);
}
String message = intent.getStringExtra("UmengMsg");
try {
UMessage msg = new UMessage(new JSONObject(message));
if (oldMessage != null) {
UTrack.getInstance(getApplicationContext()).setClearPrevMessage(true);
UTrack.getInstance(getApplicationContext()).trackMsgDismissed(oldMessage);
}
showNotification(msg);
} catch (JSONException e) {
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}

private void showNotification(UMessage msg) {
int id = new Random(System.nanoTime()).nextInt();
oldMessage = msg;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) manager.cancelAll();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder mBuilder = new Notification.Builder(this, this.getPackageName())
.setContentTitle(msg.title)
.setContentText(msg.text)
.setTicker(msg.ticker)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(getClickPendingIntent(this, msg))
.setDeleteIntent(getDismissPendingIntent(this, msg))
.setChannelId(this.getPackageName())
.setAutoCancel(true);

if (manager != null) {
// 用户可以看到的通知渠道的名字
CharSequence name = getResources().getString(R.string.app_name);
// 用户可看到的通知描述
String description = getResources().getString(R.string.app_description);
// 构建NotificationChannel实例
NotificationChannel notificationChannel =
new NotificationChannel(this.getPackageName(), name, NotificationManager.IMPORTANCE_HIGH);
// 配置通知渠道的属性
notificationChannel.setDescription(description);
// 设置通知出现时的闪光灯
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
// 设置通知出现时的震动
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 100});
// 在notificationManager中创建通知渠道
manager.createNotificationChannel(notificationChannel);
manager.notify(id, mBuilder.build());
}
} else {
Notification.Builder mBuilder = new Notification
.Builder(this)
.setContentTitle(msg.title)
.setContentText(msg.text)
.setTicker(msg.ticker)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(getClickPendingIntent(this, msg))
.setDeleteIntent(getDismissPendingIntent(this, msg))
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_ALL)
.setSound(MediaStore.Audio.Media.INTERNAL_CONTENT_URI)
.setAutoCancel(true);
if (manager != null) manager.notify(id, mBuilder.build());
}
}

public PendingIntent getClickPendingIntent(Context context, UMessage msg) {
Intent clickIntent = new Intent();
clickIntent.setClass(context, NotificationBroadcast.class);
clickIntent.putExtra(NotificationBroadcast.EXTRA_KEY_MSG,
msg.getRaw().toString());
clickIntent.putExtra(NotificationBroadcast.EXTRA_KEY_ACTION,
NotificationBroadcast.ACTION_CLICK);

return PendingIntent.getBroadcast(context,
(int) (System.currentTimeMillis()),
clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);
}

public PendingIntent getDismissPendingIntent(Context context, UMessage msg) {
Intent deleteIntent = new Intent();
deleteIntent.setClass(context, NotificationBroadcast.class);
deleteIntent.putExtra(NotificationBroadcast.EXTRA_KEY_MSG,
msg.getRaw().toString());
deleteIntent.putExtra(
NotificationBroadcast.EXTRA_KEY_ACTION,
NotificationBroadcast.ACTION_DISMISS);
return PendingIntent.getBroadcast(context,
(int) (System.currentTimeMillis() + 1),
deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT);
}

@Override
public IBinder onBind(Intent intent) {
return null;
}
}