【问题标题】:GCM Notification setAutoCancel(true) not working in Android 5.1.1GCM 通知 setAutoCancel(true) 在 Android 5.1.1 中不起作用
【发布时间】:2015-06-22 09:57:27
【问题描述】:

我正在使用此代码显示通知,但 setAutoCancel(true) 不起作用,因为当我按下通知打开应用程序时,通知仍在通知栏上,我必须使用手指手势手动删除它用于删除通知或使用通知栏中的全部清除按钮。

它在 android 4.4.2 设备上运行良好,但它在我的 nexus 7 和 5.1.1 中无法运行

怎么了?

private void generateNotification(Context context, String message) {

    Intent intent = new Intent(GCMListenerService.this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    String appName = context.getResources().getString(R.string.app_name);       

    int width;
    int height;
    if (Util.checkAndroidVersionUpperOrEqualThan(11)){      
        width = getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width); 
        height = getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
    }else{
        width = 64;
        height = 64;
    }

    image = Bitmap.createScaledBitmap(image, width, height, false);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
    .setSmallIcon(R.drawable.ic_launcher)
    .setLargeIcon(image)
    .setContentTitle(appName)
    .setContentText(message)
    .setSound(defaultSoundUri)
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setContentIntent(pendingIntent)
    .setAutoCancel(true)
    .setStyle(new NotificationCompat.BigTextStyle().bigText(message));

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

    notificationManager.notify(0,notificationBuilder.build());

}

【问题讨论】:

  • .setCategory(Notification.CATEGORY_MESSAGE) 与 setAutocancel 等所有其他选项一起使用。
  • Notification.CATEGORY_MESSAGE 适用于 API 级别 21...无法通过较低的编译 API 级别解决此问题?
  • 我尝试使用常量的值但不起作用... .setCategory("msg")
  • 您询问的是 5.1.1,这显然是 API 22 更高的版本,不要使用 Notification.CATEGORY_MESSAGE 代替“msg”
  • 我尝试了 .setCategory(NotificationCompat.CATEGORY_MESSAGE) 并没有工作....:/

标签: android push-notification google-cloud-messaging android-notifications android-notification-bar


【解决方案1】:

您必须使用 .setOngoing(true) 设置通知标志

   val builder = NotificationCompat.Builder(context, channelId)
   builder.setOngoing(true)
   notification = builder.build()
   notification.flags = Notification.FLAG_ONGOING_EVENT

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 2016-12-29
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    相关资源
    最近更新 更多