Android发送本地通知,之前很多方法都收不到通知了,更新完的 兼容api26
Intent intent = new Intent(mContext,TestActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
@SuppressLint("WrongConstant")
NotificationChannel notificationChannel = new NotificationChannel("ID", "NAME", importance);
notificationChannel.enableVibration(true);
notificationChannel.setShowBadge(true);
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(mContext, "ID");
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mBuilder.setDefaults(Notification.DEFAULT_ALL);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle("title");
mBuilder.setContentText("content");
mBuilder.setContentIntent(pendingIntent);
mBuilder.setNumber(1);
final Notification notify = mBuilder.build();
//随机id 1000-2000
final int notifyId = (int) (Math.random() * 1000 + 1000);
if (notificationManager != null) {
notificationManager.notify(notifyId, notify);
}