我在这里查看了所有其他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)由于我的通知很简单,我没有附加意图.
如果您有任何见解,请告诉我.
编辑:我的目的是在没有前景我的后台应用程序的情况下解除通知.
我正在尝试在我的Android应用程序上实现推送通知.所以没有比Googles Developers页面更好的起点了.
我想在这里学习这个教程:GCM演示应用程序.教程建议使用通过SDK Manager提供的示例代码.执行此操作并尝试发送推送通知后,当应用程序运行时,我会在屏幕上看到正在写入新推送已到达.
但是,当应用程序在后台或未运行时,我没有得到推送通知.如果我打开应用程序,屏幕上会再次显示消息.但我从来没有通过弹出和声音通知的形式.
我手动必须在android中执行此操作吗?我认为它与iOS类似,平台负责向您显示通知.
我有什么想法可以实现它吗?
我有代码:
public void AlarmStart() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 5);
Intent intent = new Intent(MainNote.this, AlarmReceiver.class);
intent.putExtra("alarm_message", "MESS");
PendingIntent sender = PendingIntent.getBroadcast(MainNote.this, 1,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
Run Code Online (Sandbox Code Playgroud)
它按时调用AlarmReceiver类.
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
NotifierHelper.sendNotification(?????, MainNote.class, "ba", "baba",
2, true, true);
} // Problem here
}
Run Code Online (Sandbox Code Playgroud)
然后NotifierHelper类:
public class NotifierHelper {
private static final int NOTIFY_1 …Run Code Online (Sandbox Code Playgroud) 我试图用GCM向手机发送推送通知,它在Android 4的设备上工作正常,但当我尝试在Android 2.3上发送通知应用程序崩溃,我收到此错误
03-13 11:44:25.994: E/AndroidRuntime(3579): FATAL EXCEPTION: IntentService[GCMIntentService-1074787013996-1]
03-13 11:44:25.994: E/AndroidRuntime(3579): java.lang.IllegalArgumentException: contentIntent required: pkg=com.itom.vreauRCA id=0 notification=Notification(vibrate=default,sound=default,defaults=0xffffffff)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.os.Parcel.readException(Parcel.java:1251)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.os.Parcel.readException(Parcel.java:1235)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.app.NotificationManager.notify(NotificationManager.java:110)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.app.NotificationManager.notify(NotificationManager.java:90)
03-13 11:44:25.994: E/AndroidRuntime(3579): at com.itom.vreauRCA.GCMIntentService.generateNotification(GCMIntentService.java:71)
03-13 11:44:25.994: E/AndroidRuntime(3579): at com.itom.vreauRCA.GCMIntentService.onMessage(GCMIntentService.java:36)
03-13 11:44:25.994: E/AndroidRuntime(3579): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.os.Looper.loop(Looper.java:123)
03-13 11:44:25.994: E/AndroidRuntime(3579): at android.os.HandlerThread.run(HandlerThread.java:60)
03-13 11:44:26.374: …Run Code Online (Sandbox Code Playgroud)