代码之家  ›  专栏  ›  技术社区  ›  Dean Blakely

在Oreo中发出通知

  •  0
  • Dean Blakely  · 技术社区  · 8 年前

    谷歌赚了一大笔 breaking change 在Oreo中-他们停止了所有应用中的通知。我现在正在处理改变我所有的应用程序,但我留下了一些问题。……

    通知现在被发布,当用户触摸通知时,它会正确触发应用程序。 然而 ,(1)通知不会消失。要删除它,用户必须删除它。另外(2)我的应用程序图标上的点在发布了几个通知后保持在一个位置。

    我正在使用bipin pandey发布的以下通知助手类:

    public class NotificationHelper {
    
        private Context mContext;
        private NotificationManager mNotificationManager;
        private NotificationCompat.Builder mBuilder;
        public static final String NOTIFICATION_CHANNEL_ID = "10001";
    
        public NotificationHelper(Context context) {
            mContext = context;
        }
    
        /**
         * Create and push the notification
         */
        public void createNotification(String title, String message)
        {
            /**Creates an explicit intent for an Activity in your app**/
            Intent resultIntent = new Intent(mContext , MainActivity.class);
            resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
            PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
                    0 /* Request code */, resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
    
            mBuilder = new NotificationCompat.Builder(mContext);
            mBuilder.setSmallIcon(R.drawable.ticon);
            mBuilder.setContentTitle(title)
                    .setContentText(message)
                    .setAutoCancel(false)
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setContentIntent(resultPendingIntent);
    
            mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
            {
                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(Color.RED);
                notificationChannel.enableVibration(true);
                notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                assert mNotificationManager != null;
                mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
                mNotificationManager.createNotificationChannel(notificationChannel);
            }
            assert mNotificationManager != null;
            mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
        }
    
    
    }
    

    下面的代码执行这个helper类

    NotificationHelper myNotify = new NotificationHelper(context);
    myNotify.createNotification("New Message",mesText);
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   ianhanniballake    8 年前

    你设置 setAutoCancel(false) ,因此在触发通知时不会自动取消通知。如果要点击通知以删除通知(以及与通知关联的点),请删除该行。

        2
  •  0
  •   Bhavita Lalwani    8 年前

    您将setAutoCancel属性保留为false。因此,当用户触摸通知时,通知不会被取消。这适用于您希望在自己的端处理删除通知的情况。

    从NotificationBuilder中删除这一行,您的逻辑应按预期工作。

    方法说明:

    公共通知。生成器设置自动取消(布尔自动取消)

    使此通知在用户触摸时自动取消。

    在此处阅读有关通知自动取消的详细信息:

    https://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel(boolean)

    推荐文章