抬头推送通知不执行弹出功能

时间:2018-11-08 23:50:02

标签: java android android-studio push-notification

我是Android的新手,并且我一直在尝试像WhatsApp一样显示“抬头行动”推送通知。

这是我的通知配置:

NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, ADMIN_CHANNEL)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_foreground))
                        .setSmallIcon(R.drawable.m_icon)
                        .setContentTitle(remoteMessage.getNotification().getTitle())
                        .setTicker(remoteMessage.getNotification().getBody())
                        .setColor(ContextCompat.getColor(getApplicationContext(), R.color.mblue))
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(remoteMessage.getNotification().getBody()))
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setContentIntent(likePendingIntent);

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

我在某篇文章中已经读到,完成此操作的关键是为通知设置高优先级,但是,它仍然对我不起作用。

2 个答案:

答案 0 :(得分:0)

问题在于,无论何时操作系统是Oroe,我们都必须添加新的通道配置。因此,我必须验证操作系统是否为Oreo,然后将通道分配给我的notificationManager:

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

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    setupChannels();
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void setupChannels(){
    CharSequence adminChannelName ="Global channel";
    String adminChannelDescription = "Notifications sent from the app admin";
    NotificationChannel adminChannel;
    adminChannel = new NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_HIGH);
    adminChannel.setDescription(adminChannelDescription);
    adminChannel.enableLights(true);
    adminChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
    adminChannel.setLightColor(Color.RED);
    adminChannel.enableVibration(true);
    if (notificationManager != null) {
        notificationManager.createNotificationChannel(adminChannel);
    }

}

答案 1 :(得分:0)

对于模拟器上的Android Pie(9),我将频道设置为“ IMPORTANCE_HIGH”,并将构建器的优先级设置为“ PRIORITY_HIGH”。

但是,我仍然无法收到提醒通知!

最后,在Android 9模拟器上,我必须:

  1. 向下滑动通知抽屉
  2. 点击“管理通知”
  3. 选择我的应用
  4. 选择我的频道
  5. 选择“行为”
  6. 启用“在屏幕上播放声音并弹出”。

现在可以了。