Notifications fail to display in Android Oreo (API 26)

前端 未结 4 840
[愿得一人]
[愿得一人] 2020-11-30 05:32

I get this message when trying to display a notification on Android O.

Use of stream types is deprecated for operations other than volume control

4条回答
  •  伪装坚强ぢ
    2020-11-30 05:57

    In Android O it's a must to use a NotificationChannel and NotificationCompat.Builder is deprecated (reference).

    Below is a sample code :

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(mContext.getApplicationContext(), "notify_001");
    Intent ii = new Intent(mContext.getApplicationContext(), RootActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, ii, 0);
    
    NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
    bigText.bigText(verseurl);
    bigText.setBigContentTitle("Today's Bible Verse");
    bigText.setSummaryText("Text in detail");
    
    mBuilder.setContentIntent(pendingIntent);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher_round);
    mBuilder.setContentTitle("Your Title");
    mBuilder.setContentText("Your text");
    mBuilder.setPriority(Notification.PRIORITY_MAX);
    mBuilder.setStyle(bigText);
    
    NotificationManager mNotificationManager =
            (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("notify_001",
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        mNotificationManager.createNotificationChannel(channel);
    }
    
    mNotificationManager.notify(0, mBuilder.build());
    

提交回复
热议问题