Android8.0通知
阿新 • • 發佈:2019-01-07
android裡面經常會使用Notification來顯示通知的訊息,一般使用NotificationManager來建立通知訊息
NotificationManager manger = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); //為了版本相容 選擇V7包下的NotificationCompat進行構造 NotificationCompat.Builder builder = new NotificationCompat.Builder(context); //Ticker是狀態列顯示的提示 builder.setTicker("1"); //第一行內容 通常作為通知欄標題 builder.setContentTitle("推送標題"); //第二行內容 通常是通知正文 builder.setContentText("內容"); //可以點選通知欄的刪除按鈕刪除 builder.setAutoCancel(true); //系統狀態列顯示的小圖示 builder.setSmallIcon(R.mipmap.ic_launcher); Notification notification = builder.build(); notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.ti); builder.setDefaults(NotificationCompat.DEFAULT_VIBRATE | NotificationCompat.DEFAULT_LIGHTS); notification.flags |= Notification.FLAG_AUTO_CANCEL; // Intent clickIntent = new Intent(); //點選通知之後要傳送的廣播 // int id = (int) (System.currentTimeMillis() / 1000); // clickIntent.addCategory(context.getPackageName()); // clickIntent.setAction(JPushInterface.ACTION_NOTIFICATION_OPENED); // clickIntent.putExtra(JPushInterface.EXTRA_EXTRA, bundle.getString(JPushInterface.EXTRA_EXTRA)); // PendingIntent contentIntent = PendingIntent.getBroadcast(context, id, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); // notification.contentIntent = contentIntent; manger.notify(1, notification);
然而在Android8.0以上的版本並不能看到通知的內容,android8.0需要使用NotificationChannel來處理通知的顯示,根據處理得到了以下內容。
package com.snail.monitor.util; import android.annotation.TargetApi; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Build; import android.support.v4.app.NotificationCompat; import com.snail.monitor.R; import cn.jpush.android.api.JPushInterface; import static android.app.Notification.VISIBILITY_SECRET; import static android.support.v4.app.NotificationCompat.PRIORITY_DEFAULT; /** * Created by zd on 2019/1/7. */ public class NotificationUtils extends ContextWrapper { public static final String CHANNEL_ID = "default"; private static final String CHANNEL_NAME = "Default Channel"; private static final String CHANNEL_DESCRIPTION = "this is default channel!"; private NotificationManager mManager; private Context context; private String EXTRA_EXTRA; public NotificationUtils(Context base,String EXTRA_EXTRA) { super(base); context = base; this.EXTRA_EXTRA = EXTRA_EXTRA; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createNotificationChannel(); } } @TargetApi(Build.VERSION_CODES.O) private void createNotificationChannel() { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT); //是否繞過請勿打擾模式 channel.canBypassDnd(); //閃光燈 channel.enableLights(true); //鎖屏顯示通知 channel.setLockscreenVisibility(VISIBILITY_SECRET); //閃關燈的燈光顏色 channel.setLightColor(Color.RED); //桌面launcher的訊息角標 channel.canShowBadge(); //是否允許震動 channel.enableVibration(true); //獲取系統通知響鈴聲音的配置 channel.getAudioAttributes(); //獲取通知取到組 channel.getGroup(); //設定可繞過 請勿打擾模式 channel.setBypassDnd(true); //設定震動模式 channel.setVibrationPattern(new long[]{100, 100, 200}); //是否會有燈光 channel.shouldShowLights(); getManager().createNotificationChannel(channel); } private NotificationManager getManager() { if (mManager == null) { mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } return mManager; } /** * 傳送通知 */ public void sendNotification(String title, String content) { NotificationCompat.Builder builder = getNotification(title, content); getManager().notify(1, builder.build()); } private NotificationCompat.Builder getNotification(String title, String content) { NotificationCompat.Builder builder = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID); } else { builder = new NotificationCompat.Builder(getApplicationContext()); builder.setPriority(PRIORITY_DEFAULT); } //標題 builder.setContentTitle(title); //文字內容 builder.setContentText(content); //小圖示 builder.setSmallIcon(R.mipmap.ic_launcher); //設定點選資訊後自動清除通知 builder.setAutoCancel(true); Intent clickIntent = new Intent(); //點選通知之後要傳送的廣播 int id = (int) (System.currentTimeMillis() / 1000); clickIntent.addCategory(context.getPackageName()); clickIntent.setAction(JPushInterface.ACTION_NOTIFICATION_OPENED); clickIntent.putExtra(JPushInterface.EXTRA_EXTRA, EXTRA_EXTRA); PendingIntent contentIntent = PendingIntent.getBroadcast(context, id, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); return builder; } /** * 傳送通知 */ public void sendNotification(int notifyId, String title, String content) { NotificationCompat.Builder builder = getNotification(title, content); getManager().notify(notifyId, builder.build()); } }