Bec*_*caP 19 java notifications android
我在这里查看了所有其他AUTO-CANCEL无法解决的问题,而且它们似乎都涉及到我没有犯的错误.我试过了两个
builder.setAutoCancel(true);
Run Code Online (Sandbox Code Playgroud)
和
Notification notif = builder.build();
notif.flags |= Notification.FLAG_AUTO_CANCEL;
Run Code Online (Sandbox Code Playgroud)
两者都不起作用.
我使用的是NotificationCompat,因为我的最小API是8.这是我的完整代码.在这个特定的通知中,我没有调用意图,因为我不需要用户做任何事情.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(getString(R.string.app_name) + ": my title");
builder.setContentText(message);
builder.setSmallIcon(R.drawable.notification_icon);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.prog_icon);
builder.setLargeIcon(bitmap);
builder.setAutoCancel(true); // dismiss notification on user click
NotificationManager notiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notiManager.notify(MY_NOTI_MANAGER_ID, builder.build());
Run Code Online (Sandbox Code Playgroud)
通知显示完美.您可以滑动以清除它.但只是点击它并不会忽略通知.它只是点亮并留在那里.
我的代码和其他人在这里发布的一些可能的差异:1)我正在使用NotificationCompat(这不应该有所作为,但我们之前已经听过).2)由于我的通知很简单,我没有附加意图.
如果您有任何见解,请告诉我.
编辑:我的目的是在没有前景我的后台应用程序的情况下解除通知.
Bec*_*caP 28
显然你确实需要一个未决的意图.
在Android - 通知管理器,没有意图的通知,我找到了一个解决方案,将当前活动的应用程序作为您的待处理意图(这样您就不必开始自己的活动以解除通知).
我刚刚添加了以下两行代码(在设置自动取消后立即):
PendingIntent notifyPIntent =
PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);
builder.setContentIntent(notifyPIntent);
Run Code Online (Sandbox Code Playgroud)
它运作得很好.我会说,如果您不希望由于用户点击您的通知而重新启动您的活动,那么这是您的最佳选择.
你似乎错过了PendingIntent和setContentIntent()打电话.我认为这是自动取消工作所必需的.
以下是此示例项目中的一些显示Notification逻辑:
private void raiseNotification(Intent inbound, File output, Exception e) {
NotificationCompat.Builder b=new NotificationCompat.Builder(this);
b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis());
if (e == null) {
b.setContentTitle(getString(R.string.download_complete))
.setContentText(getString(R.string.fun))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setTicker(getString(R.string.download_complete));
Intent outbound=new Intent(Intent.ACTION_VIEW);
outbound.setDataAndType(Uri.fromFile(output), inbound.getType());
b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0));
}
else {
b.setContentTitle(getString(R.string.exception))
.setContentText(e.getMessage())
.setSmallIcon(android.R.drawable.stat_notify_error)
.setTicker(getString(R.string.exception));
}
NotificationManager mgr=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mgr.notify(NOTIFY_ID, b.build());
}
Run Code Online (Sandbox Code Playgroud)
亲爱的朋友,如果您想在特定时间显示不可取消的通知(用户不可取消),然后您需要清除它(如音乐播放器),您可以使用它。
mNotificationBuilder .setSmallIcon(android.R.drawable.btn_plus);
mNotificationBuilder .setContentTitle("My notification");
mNotificationBuilder .setContentText("Notificattion From service");
mNotificationBuilder .setLights(0xFF0000FF, 500, 500);
Notification note = mNotificationBuilder.build();
note.flags = Notification.FLAG_ONGOING_EVENT; // For Non cancellable notification
mNotificationManager.notify(NOTIFICATION_ID, note);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29102 次 |
| 最近记录: |