【发布时间】:2016-05-16 16:59:19
【问题描述】:
您好,我正在开发一个应用程序,我想在收到短信时向用户发送推送通知。现在的问题是,当用户通过启动器图标打开应用程序时,通知图标仍然没有删除 这是我的代码:
NotificationCompat.Builder notify = new NotificationCompat.Builder(context);
notify.setSmallIcon(R.drawable.appicon);
notify.setContentTitle(title);
notify.setContentText(msgBody);
notify.setAutoCancel(true);
Notification notification = new Notification();
notification.defaults |= Notification.DEFAULT_VIBRATE;
//notify.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notify.setLights(Color.GREEN, 2000, 2000);
notify.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_DEFAULT);
notificationIntent.setType("vnd.android-dir/mms-sms");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intentt = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notify.setContentIntent(intentt);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notify.build());
我也试过这样:
notificationManager.cancel(0);
也试过这个:
notificationManager.cancelAll();
但这两个都不起作用。他们不允许发生通知。也许他们在创建推送通知之前取消了它。 请帮忙!
【问题讨论】:
-
您是否尝试将取消代码放在 onCreate 中?
-
@MorZa 不,先生,我也需要吗?但是如果我写了那个并且如果没有通知并且我打开我的应用程序那么它可能会给出空异常?请解释
-
在我的代码中,我得到了意图,然后检查它是否为空: Intent intent = getIntent();如果(意图!= null){...}。将它放在 onCreate 或 onResume 对我来说是有意义的,因为用户在按下通知时打开应用程序并调用这些方法。
标签: android push-notification sms launcher