Android O中的通知渠道问题

53

当我使用Android Messages版本2.3.063发送MMS时,我会收到一个名为“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
Android 8.0.0

有人能帮我吗?屏幕截图


我看了这个 https://dev59.com/vFcP5IYBdhLWcg3wYo_i 但是对我都没有用。 - Shabbir Panjesha
如果应用程序的目标是Android O,则必须使用通知频道发布所有通知。否则,通知将被丢弃,并且在运行Android O的设备上会显示“开发者警告”Toast消息。 - Bob
但我相信Android消息应用程序已经添加了通知渠道支持。 - Bob
分享代码片段,展示你是如何显示通知的。 - azizbekian
如果您正在使用API级别26+,请参考以下相关帖子 - NotificationCompat.Builder不接受第二个参数 - RBT
3个回答

87

正如在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);

实际上,您需要使用通知构建器来通过setChannelId()设置频道ID。


什么是 Notification 中的 .setChannelId? - Mohit Singh
2
它只是将您刚创建的通知通道绑定到通知构建器。 - Milad Moosavi
我发布了我的问题,请帮帮我 https://dev59.com/wlcO5IYBdhLWcg3wgRsG - Mohit Singh
@Ridcully 这取决于您在哪里进行通知。它可以是活动的onCreate()或片段的onCreateView()。 - Milad Moosavi
1
我之前遇到了一个错误:Wrong 1st argument type. Found: 'java.lang.String', required: 'int',它出现在这一行代码中:mNotificationManager.notify(id, notification);。所以我改成传递 notifyID 而不是 id,现在我可以正常接收通知了。 - Hunterr
显示剩余4条评论

6

Toast和Logcat中的消息提醒您应该注意以下2个项目以及它们的顺序:

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

同时,NotificationManager notifManager和NotificationChannel mChannel只需要创建一次。

通知需要设置必需的setter:

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

请参考Android 8.1 API 27通知无法显示中的示例。

0

简单易懂

companion object {
    private val TAG = simpleClassName

    // The id of the channel.
    private val channelId = "my_channel_01"
    private val notifyID: Int = 1
    private val importance = NotificationManagerCompat.IMPORTANCE_DEFAULT
}


val mChannel = NotificationChannelCompat.Builder(channelId, importance).apply {
    setName("channel name") // Must set! Don't remove
    setDescription("channel description")
    setLightsEnabled(true)
    setLightColor(Color.RED)
    setVibrationEnabled(true)
    setVibrationPattern(longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400))
}.build()


NotificationManagerCompat.from(context).createNotificationChannel(mChannel)
val notification: Notification = NotificationCompat.Builder(context, channelId)
    .setSmallIcon(R.drawable.ic_launcher_foreground)
    .setContentTitle(sender)
    .setContentText(body)
    .build()
NotificationManagerCompat.from(context).notify(notifyID, notification)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接