2017-08-02 51 views
46

我的項目升級到Android的Ø後NotificationCompat.Builder Android中棄用Ø

buildToolsVersion "26.0.1" 

皮棉Android Studio中被顯示爲後續通知建設者方法已過時的警告:

new NotificationCompat.Builder(context) 

問題是: Android開發人員更新其文檔描述NotificationChannel以支持否tifications在Android的O,併爲我們提供了一個片段,但與同棄用警告:

Notification notification = new Notification.Builder(MainActivity.this) 
     .setContentTitle("New Message") 
     .setContentText("You've received new messages.") 
     .setSmallIcon(R.drawable.ic_notify_status) 
     .setChannelId(CHANNEL_ID) 
     .build(); 

Notifications Overview

我的問題:有是構建通知任何其他解決方案,而且還支持Android Ø?

我找到的解決方案是將通道ID作爲Notification.Builder構造函數中的參數傳遞。但是這個解決方案並不是完全可重用的。

new Notification.Builder(MainActivity.this, "channel_id") 
+3

*但是這個解決方案並不是完全可重用的。 –

+1

NotificationCompat.Builder不推薦使用Notification.Builder。注意Compat部分消失了。通知是他們的新課程,他們正在精簡所有內容 –

+0

@kapsym實際上是另一種方式。 Notification.Builder較老 –

回答

37

在文檔中提到了構建器方法NotificationCompat.Builder(Context context)已被棄用。我們不得不使用它具有channelId參數構造函數:

NotificationCompat.Builder(Context context, String channelId) 

https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html

此構造函數在API層面26.0.0-β1已被否決。改爲使用 NotificationCompat.Builder(上下文,字符串)。全部發布 通知必須指定NotificationChannel標識。

https://developer.android.com/reference/android/app/Notification.Builder.html

此構造在代替API級26使用 Notification.Builder(上下文,字符串)棄用。全部發布 通知必須指定NotificationChannel標識。

如果要重用構建器設置器,可以使用channelId創建構建器,並將該構建器傳遞給輔助方法,並在該方法中設置首選設置。

+2

在NotificationChannel會話中發佈Notification.Builder(上下文)解決方案時,它們似乎有矛盾。但是,至少你發現了一個帖子,通知這個棄用=) – GuilhermeFGL

+4

你可以解釋什麼是channelId? –

+2

什麼是channelId? – RoundTwo

16

調用2-arg構造函數:爲了與Android O兼容,請致電support-v4 NotificationCompat.Builder(Context context, String channelId)。在Android N或更低版本上運行時,channelId將被忽略。在Android O上運行時,也會使用相同的channelId創建NotificationChannel

過時的示例代碼:示例代碼在幾個JavaDoc的頁面,例如Notification.Builder調用new Notification.Builder(mContext)是過時的。

已過時的構造函數:Notification.Builder(Context context)V4NotificationCompat.Builder(Context context)被棄用,取而代之的Notification[Compat].Builder(Context context, String channelId)。 (參見Notification.Builder(android.content.Context)和v4 NotificationCompat.Builder(Context context)。)

棄用類:整個類V7NotificationCompat.Builder已棄用。 (請參閱v7 NotificationCompat.Builder。)之前,需要v7 NotificationCompat.Builder才能支持NotificationCompat.MediaStyle。在Android O中,media-compat libraryandroid.support.v4.media包中有一個v4 NotificationCompat.MediaStyle。如果你需要MediaStyle,請使用那個。

API 14+:在26.0.0和更高版本的支持庫中,support-v4和support-v7軟件包均支持最小API級別爲14.v#名稱爲歷史記錄。

請參閱Recent Support Library Revisions

25

這是所有安卓版本的工作代碼,如接受的答案中所述。

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext(), "M_CH_ID"); 

     notificationBuilder.setAutoCancel(true) 
       .setDefaults(Notification.DEFAULT_ALL) 
       .setWhen(System.currentTimeMillis()) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setTicker("Hearty365") 
       .setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API 
       .setContentTitle("Default notification") 
       .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.") 
       .setContentInfo("Info"); 

NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify(1, notificationBuilder.build()); 

更新API 26來設置最大優先

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    String NOTIFICATION_CHANNEL_ID = "my_channel_id_01"; 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
     NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX); 

     // Configure the notification channel. 
     notificationChannel.setDescription("Channel description"); 
     notificationChannel.enableLights(true); 
     notificationChannel.setLightColor(Color.RED); 
     notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000}); 
     notificationChannel.enableVibration(true); 
     notificationManager.createNotificationChannel(notificationChannel); 
    } 


    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); 

    notificationBuilder.setAutoCancel(true) 
      .setDefaults(Notification.DEFAULT_ALL) 
      .setWhen(System.currentTimeMillis()) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setTicker("Hearty365") 
     //  .setPriority(Notification.PRIORITY_MAX) 
      .setContentTitle("Default notification") 
      .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.") 
      .setContentInfo("Info"); 

    notificationManager.notify(/*notification id*/1, notificationBuilder.build()); 
+0

您在代碼中使用了兩次'setDefaults'。 – GuilhermeFGL

+0

@GuilhermeFGL謝謝指出。我已經更新了我的回答 – Aks4125

+0

如何讓通知實際顯示在應用程序的屏幕上,或者如果在另一個應用程序中? – BlueBoy

2

下面是示例代碼,它是工作在的Android奧利奧和小於奧利奧。

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      NotificationCompat.Builder builder = null; 
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 
       int importance = NotificationManager.IMPORTANCE_DEFAULT; 
       NotificationChannel notificationChannel = new NotificationChannel("ID", "Name", importance); 
       notificationManager.createNotificationChannel(notificationChannel); 
       builder = new NotificationCompat.Builder(getApplicationContext(), notificationChannel.getId()); 
      } else { 
       builder = new NotificationCompat.Builder(getApplicationContext()); 
      } 

      builder = builder 
        .setSmallIcon(R.drawable.ic_notification_icon) 
        .setColor(ContextCompat.getColor(context, R.color.color)) 
        .setContentTitle(context.getString(R.string.getTitel)) 
        .setTicker(context.getString(R.string.text)) 
        .setContentText(message) 
        .setDefaults(Notification.DEFAULT_ALL) 
        .setAutoCancel(true); 
      notificationManager.notify(requestCode, builder.build()); 
9

不是檢查Build.VERSION.SDK_INT >= Build.VERSION_CODES.O許多答案的提示,有一個稍微簡單的方式 -

以下行添加到作爲Set Up a Firebase Cloud Messaging Client App on Android文檔解釋的AndroidManifest.xml文件的application部分:

<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" 
     android:value="@string/default_notification_channel_id" /> 

然後用頻道名稱添加一行到值/ strings.xml中文件:

<string name="default_notification_channel_id">default</string> 

之後,你將能夠使用NotificationCompat.Builder構造的新版本有兩個參數(因爲舊的構造以1個參數已經在Android的奧利奧被棄用):

private void sendNotification(String title, String body) { 
    Intent i = new Intent(this, MainActivity.class); 
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pi = PendingIntent.getActivity(this, 
      0 /* Request code */, 
      i, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, 
     getString(R.string.default_notification_channel_id)) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle(title) 
      .setContentText(body) 
      .setAutoCancel(true) 
      .setSound(sound) 
      .setContentIntent(pi); 

    NotificationManager manager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    manager.notify(0, builder.build()); 
} 
2

簡單示例

public void showNotification (String from, String notification, Intent intent) { 
     PendingIntent pendingIntent = PendingIntent.getActivity(
       context, 
       Notification_ID, 
       intent, 
       PendingIntent.FLAG_UPDATE_CURRENT 
     ); 


     String NOTIFICATION_CHANNEL_ID = "my_channel_id_01"; 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 


     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
      NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT); 

      // Configure the notification channel. 
      notificationChannel.setDescription("Channel description"); 
      notificationChannel.enableLights(true); 
      notificationChannel.setLightColor(Color.RED); 
      notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000}); 
      notificationChannel.enableVibration(true); 
      notificationManager.createNotificationChannel(notificationChannel); 
     } 


     NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID); 
     Notification mNotification = builder 
       .setContentTitle(from) 
       .setContentText(notification) 

//    .setTicker("Hearty365") 
//    .setContentInfo("Info") 
       //  .setPriority(Notification.PRIORITY_MAX) 

       .setContentIntent(pendingIntent) 

       .setAutoCancel(true) 
//    .setDefaults(Notification.DEFAULT_ALL) 
//    .setWhen(System.currentTimeMillis()) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) 
       .build(); 

     notificationManager.notify(/*notification id*/Notification_ID, mNotification); 

    } 
相關問題