0

我目前正在尝试在 Nexus S (API 29) 上获取标题通知,但没有成功。我见过一些具有各种解决方案的线程,但没有一个有效。代码如下:

Context applicationContext = getApplicationContext();    
NotificationCompat.Builder builder = new NotificationCompat.Builder(applicationContext, CHANNEL_ID)
                                .setDefaults(NotificationCompat.DEFAULT_ALL)
                                .setDefaults(NotificationCompat.DEFAULT_SOUND)
                                .setSmallIcon(R.drawable.ic_account_circle)
                                .setContentTitle(notificationTitle)
                                .setContentText(notificationTextBody)
                                .setPriority(NotificationCompat.PRIORITY_HIGH)
                                .setLights(Color.RED, 3000, 3000);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(applicationContext);



int notificationId = new Random().nextInt();
//launches the notification on the device
notificationManager.notify(notificationId, builder.build());

我试过设置声音,振动,将优先级设置为高或最大。我什至使用了这种方法setFullScreenIntent,单独地一次全部使用,但没有完成我所寻求的。

4

1 回答 1

0

在 Android 8+ 上,您必须创建通知通道,例如在DOC中

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

请注意,渠道也很重要。如果你想要一个提醒通知,你必须为此频道设置高优先级,DOC

可能触发提醒通知的示例条件包括:

通知通道在运行 Android 8.0(API 级别 26)及更高版本的设备上非常重要。

于 2021-09-28T12:56:18.723 回答