【问题标题】:NotificationChannel issue in Android OAndroid O 中的 NotificationChannel 问题
【发布时间】:2017-08-14 05:45:08
【问题描述】:

在使用 Android 消息 2.3.063 版发送彩信时,我收到一个祝酒词“开发者警告包 com.google.android.apps.messaging”。

在日志中

08-12 16:57:52.368  7661  7682 W Notification: Use of stream types is deprecated for operations other than volume control
08-12 16:57:52.368  7661  7682 W Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
08-12 16:57:52.369  1604  3146 E NotificationService: No Channel found for pkg=com.google.android.apps.messaging, channelId=miscellaneous, id=5, tag=null, opPkg=com.google.android.apps.messaging, callingUid=10130, userId=0, incomingUserId=0, notificationUid=10130, notification=Notification(channel=miscellaneous pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x8 color=0xff2a56c6 vis=PRIVATE)
08-12 16:57:52.375  1604  3094 D CompatibilityInfo: mCompatibilityFlags - 0
08-12 16:57:52.375  1604  3094 D CompatibilityInfo: applicationDensity - 480
08-12 16:57:52.375  1604  3094 D CompatibilityInfo: applicationScale - 1.0
08-12 16:57:52.378  7661  7682 I BugleNotifications: Notifying for tag = null, type = RESIZING_NOTIFICATION_ID, notification = Notification(channel=miscellaneous pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x8 color=0xff2a56c6 vis=PRIVATE)
08-12 16:57:52.381  7661  8893 W Notification: Use of stream types is deprecated for operations other than volume control
08-12 16:57:52.381  7661  8893 W Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
08-12 16:57:52.384  1604  1618 E NotificationService: No Channel found for pkg=com.google.android.apps.messaging, channelId=miscellaneous, id=5, tag=null, opPkg=com.google.android.apps.messaging, callingUid=10130, userId=0, incomingUserId=0, notificationUid=10130, notification=Notification(channel=miscellaneous pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x8 color=0xff2a56c6 vis=PRIVATE)
08-12 16:57:52.384   880  1657 W StreamHAL: Error from HAL stream in function get_presentation_position: Operation not permitted
08-12 16:57:52.387  7661  8893 I BugleNotifications: Notifying for tag = null, type = RESIZING_NOTIFICATION_ID, notification = Notification(channel=miscellaneous pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x8 color=0xff2a56c6 vis=PRIVATE)
08-12 16:57:52.390  1604  1647 E NotificationService: No Channel found for pkg=com.google.android.apps.messaging, channelId=miscellaneous, id=5, tag=null, opPkg=com.google.android.apps.messaging, callingUid=10130, userId=0, incomingUserId=0, notificationUid=10130, notification=Notification(channel=miscellaneous pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x48 color=0xff2a56c6 vis=PRIVATE)

Google Play 服务 11.3.02 版
Android 消息 2.3.063
安卓 8.0.0

有人帮我吗?

【问题讨论】:

标签: android android-notifications android-8.0-oreo


【解决方案1】:

正如它在 Android 文档中所写的那样:

https://developer.android.com/preview/features/notification-channels.html

如果您以 Android O 为目标并在未指定有效通知渠道的情况下发布通知,则通知将无法发布并且系统会记录错误。

要解决这个问题,您需要创建一个 NotificationChannel。

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

// The id of the channel.
String id = "my_channel_01";

// The user-visible name of the channel.
CharSequence name = getString(R.string.channel_name);

// The user-visible description of the channel.
String description = getString(R.string.channel_description);

int importance = NotificationManager.IMPORTANCE_LOW;

NotificationChannel mChannel = new NotificationChannel(id, name,importance);

// Configure the notification channel.
mChannel.setDescription(description);

mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);

mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});

mNotificationManager.createNotificationChannel(mChannel);

然后像这样将其分配给您的通知:

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

// Sets an ID for the notification, so it can be updated.
int notifyID = 1;

// The id of the channel.
String CHANNEL_ID = "my_channel_01";

// Create a notification and set the notification channel.
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();

// Issue the notification.
mNotificationManager.notify(id, notification);

更新:

如果你想在这里使用 NotificationCompat 是一个简单的例子:

NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(getApplicationContext())
    .setSmallIcon(R.mipmap.ic_launcher_icon)
    .setContentTitle("Title")
    .setContentText("Text")
    .setOngoing(true)
    .setChannelId(id);

事实上,您必须使用 Notification Builder 才能通过 setChannelId(); 设置频道 ID;

【讨论】:

  • Notification 中的 .setChannelId 是什么
  • 它只是将你刚刚创建的通知通道绑定到了notification.builder。
  • 我应该在何处/何时创建通知渠道?一次,在应用程序启动时?在 App#onCreate() 中?
  • 我收到一个错误:Wrong 1st argument type. Found: 'java.lang.String', required: 'int'mNotificationManager.notify(id, notification); 行中,所以在通过notifyID 而不是id 之后,我现在收到了通知。
【解决方案2】:

Toast 和 Logcat 上的消息说你应该注意 2 项和它们的顺序:

  1. NotificationChannel mChannel = new NotificationChannel(id, name, importance);
  2. builder = new NotificationCompat.Builder(this, id);

NotifManager 和 NotificationChannel mChannel 也只创建一次。

Notification 需要设置器:

builder.setContentTitle() // required  
       .setSmallIcon()    // required 
       .setContentText()  // required  

请参阅On Android 8.1 API 27 notification does not display 中的示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    相关资源
    最近更新 更多