简体   繁体   English

注意推送通知不弹出 function

[英]Heads up push notification not doing the Pop up function

I am new to Android and I have been trying to show a Heads up action push notification just like whatsapp does.我是 Android 的新手,我一直在尝试像 whatsapp 一样显示 Heads up 动作推送通知。

This is my configuration for my notification:这是我的通知配置:

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());

I have read in some post that the key to acomplish this is to set high priority to the notification but, It is still not working for me.我在一些帖子中读到,完成此操作的关键是为通知设置高优先级,但它仍然对我不起作用。

The issue is that we have to add a new channel configuration whenever the OS is Oroe. 问题是,无论何时操作系统为Oroe,我们都必须添加新的通道配置。 So I had to validate if the OS is Oreo and then asign a channel to my notificationManager: 因此,我必须验证操作系统是否为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);
    }

}

For Android Pie (9) on the emulator, I setup my channel with "IMPORTANCE_HIGH", and also set my builder priority to "PRIORITY_HIGH". 对于模拟器上的Android Pie(9),我将频道设置为“ IMPORTANCE_HIGH”,还将构建器的优先级设置为“ PRIORITY_HIGH”。

But, I still could not get heads-up notifications! 但是,我仍然无法收到提醒通知!

Finally, on the Android 9 emulator, I had to: 最后,在Android 9模拟器上,我必须:

  1. swipe down the notifications drawer 向下滑动通知抽屉
  2. tap on "Manage Notifications" 点击“管理通知”
  3. select my app 选择我的应用
  4. select my channel 选择我的频道
  5. select "Behavior" 选择“行为”
  6. enable "Make sound and pop on screen". 启用“在屏幕上播放声音并弹出”。

Now it works. 现在可以了。

Just Add This Line In Your Notification Channel Builder:-只需在您的通知通道构建器中添加此行:-

channel.setShowBadge(true) channel.setShowBadge(真)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM