推送弹窗
兼容8.0
private void showNotification(Context context, String title, String content, PendingIntent pendingIntent) {
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
notificationChannel.enableVibration(true);
notificationChannel.setShowBadge(false);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
mBuilder.setSmallIcon(SmallIconCompat.getSmallIcon());
mBuilder.setTicker(title);
mBuilder.setContentTitle(title);
mBuilder.setContentText(content);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setNumber(1);
final Notification notify = mBuilder.build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notify.defaults = Notification.DEFAULT_SOUND; // 调用系统自带声音
//随机id 1000-2000
final int notifyId = (int) (Math.random() * 1000 + 1000);
if (notificationManager != null) {
notificationManager.notify(notifyId, notify);
}
}
推送点击处理
当点击推送时,会跳转到pendingintent
指定的页面。
但是页面太多,我们想封装一个中转类,来跳转不同的Activity
public class PushTransActivity extends Activity {
/**
* 将类的getCanonicalName 传过来 通过反射获取Class
*/
public static final String PUSH_TO_WHERE = "PUSH_TO_WHERE";
public static final String PUSH_MODEL = "PUSH_MODEL";
public static final String PUSH_ROUTER = "PUSH_ROUTER";
public static final String PUSH_JSON = "PUSH_JSON";
public static final String PUSH_SOURCE = "PUSH_SOURCE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_push);
Intent intent = getIntent();
if (intent == null) {
this.finish();
return;
}
Bundle extras = intent.getExtras();
Intent notifyIntent = null;
String toWhere = intent.getStringExtra(PUSH_TO_WHERE);
if (BuildConfig.DEBUG) {
Logger.d("onReceive---> toWhere: %s --extras-- %s"
, toWhere, extras);
}
if (TextUtils.isEmpty(toWhere)) {
this.finish();
return;
}
try {
Class<?> aClass = Class.forName(toWhere);
notifyIntent = new Intent(this, aClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (notifyIntent != null) {
if (extras != null) {
notifyIntent.putExtras(extras);
}
startActivity(notifyIntent);
}
this.finish();
}
}
PushTransActivity是透明的activity,跳转此Activity,会携带相关数据
PUSH_TO_WHERE
是跳转到哪个Activity。当然,此Activity需要携带bundle的话可以支持的
Bundle extras = intent.getExtras();
notifyIntent.putExtras(extras);
有了这个中转Activity,我们就可以跳转任意Activity了
示例:
private void goLaunchPage(Context context, String msgJson, boolean showNotification, BasePushMsgEntity basePush, String source) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
Intent msgIntent = new Intent(context, PushTransActivity.class);
msgIntent.putExtra(PushTransActivity.PUSH_TO_WHERE, QueenLauncherActivity.class.getCanonicalName());
msgIntent.putExtra(PushTransActivity.PUSH_JSON, msgJson);
msgIntent.putExtra(PushTransActivity.PUSH_SOURCE, source);
stackBuilder.addNextIntent(msgIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
if (showNotification) {
showNotification(context, basePush.getTitle(), basePush.getBody(), pendingIntent);
} else {
try {
assert pendingIntent != null;
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
}
网友评论