溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android 8.0實現(xiàn)發(fā)送通知的方法

發(fā)布時間:2020-07-30 09:34:40 來源:億速云 閱讀:271 作者:小豬 欄目:開發(fā)技術(shù)

這篇文章主要講解了Android 8.0實現(xiàn)發(fā)送通知的方法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

在Android8.0以后,針對Notification 通知api做了修改,新增了通知渠道(NotificationCannel)。下面就把demo的詳細代碼記錄下:

1.Application 為NotificationManager添加通知頻道

import android.app.Application;

import com.xinrui.ndkapp.notification.NotificationChannels;

public class NdkApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    NotificationChannels.createAllNotificationChannels(this);
  }
}

2.NotificationChannels 類

public class NotificationChannels {
  public final static String CRITICAL = "critical";
  public final static String IMPORTANCE = "importance";
  public final static String DEFAULT = "default";
  public final static String LOW = "low";
  public final static String MEDIA = "media";

  public static void createAllNotificationChannels(Context context) {
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if(nm == null) {
      return;
    }

    NotificationChannel mediaChannel = new NotificationChannel(
        MEDIA,
        context.getString(R.string.app_name),
        NotificationManager.IMPORTANCE_DEFAULT);
    mediaChannel.setSound(null,null);
    mediaChannel.setVibrationPattern(null);

    nm.createNotificationChannels(Arrays.asList(
        new NotificationChannel(
            CRITICAL,
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_HIGH),
        new NotificationChannel(
            IMPORTANCE,
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_DEFAULT),
        new NotificationChannel(
            DEFAULT,
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_LOW),
        new NotificationChannel(
            LOW,
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_MIN),
        //custom notification channel
        mediaChannel
    ));
  }
}

3.發(fā)送通知

public void sendSimpleNotification(Context context, NotificationManager nm) {
    //創(chuàng)建點擊通知時發(fā)送的廣播
    Intent intent = new Intent(context, NotificationMonitorService.class);
    intent.setAction("android.service.notification.NotificationListenerService");
    PendingIntent pi = PendingIntent.getService(context,0,intent,0);
    //創(chuàng)建刪除通知時發(fā)送的廣播
    Intent deleteIntent = new Intent(context,NotificationMonitorService.class);
    deleteIntent.setAction(Intent.ACTION_DELETE);
    PendingIntent deletePendingIntent = PendingIntent.getService(context,0,deleteIntent,0);
    //創(chuàng)建通知
    Notification.Builder nb = new Notification.Builder(context, NotificationChannels.DEFAULT)
        //設(shè)置通知左側(cè)的小圖標
        .setSmallIcon(R.drawable.ic_notification)
        //設(shè)置通知標題
        .setContentTitle("Simple notification")
        //設(shè)置通知內(nèi)容
        .setContentText("Demo for simple notification!")
        //設(shè)置點擊通知后自動刪除通知
        .setAutoCancel(true)
        //設(shè)置顯示通知時間
        .setShowWhen(true)
        //設(shè)置通知右側(cè)的大圖標
        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_notifiation_big))
        //設(shè)置點擊通知時的響應(yīng)事件
        .setContentIntent(pi)
        //設(shè)置刪除通知時的響應(yīng)事件
        .setDeleteIntent(deletePendingIntent);
    //發(fā)送通知
    nm.notify(Notificaitons.NOTIFICATION_SAMPLE,nb.build());
 }

看完上述內(nèi)容,是不是對Android 8.0實現(xiàn)發(fā)送通知的方法有進一步的了解,如果還想學習更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI