Ich verwende diesen Code, um die lokale Benachrichtigung anzuzeigen. Wenn eine Benachrichtigung angezeigt wird, kann ListActivity bei einem Klick auf Benachrichtigung gestartet werden. Auf dem nexus-Gerät von Google wird ListActiviy
jedoch nicht gestartet, wenn die Benachrichtigung angeklickt wird. Dieser Code funktioniert jedoch auf anderen Geräten.
Intent notificationIntent = new Intent(context,
ListActivity.class);
notificationIntent.putExtra("clicked", "Notification Clicked");
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); // To open only one activity on launch.
PendingIntent pIntent = PendingIntent.getActivity(context, reqCode,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager nM = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notify = new NotificationCompat.Builder(
context);
notify.setContentIntent(pIntent);
notify.setSmallIcon(R.drawable.app_icon);
notify.setContentTitle("Hello World");
notify.setContentText("");
notify.setAutoCancel(true);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notify.setSound(alarmSound);
notify.setLights(Color.BLUE, 500, 1000);
nM.notify(reqCode, notify.build());
Logcat hinzufügen, wenn die Aktivität nicht gestartet wird:
03-26 14:22:35.893: W/ActivityManager(515): Permission Denial: starting Intent { cmp=com.x.y/.music.ui.PlaybackActivity bnds=[42,101][636,186] (has extras) } from null (pid=-1, uid=10121) not exported from uid 10126
03-26 14:22:35.893: W/ActivityManager(515): Unable to send startActivity intent
03-26 14:22:35.893: W/ActivityManager(515): Java.lang.SecurityException: Permission Denial: starting Intent { cmp=com.x.y/.music.ui.PlaybackActivity bnds=[42,101][636,186] (has extras) } from null (pid=-1, uid=10121) not exported from uid 10126
03-26 14:22:35.893: W/ActivityManager(515): at com.Android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.Java:1186)
03-26 14:22:35.893: W/ActivityManager(515): at com.Android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.Java:741)
03-26 14:22:35.893: W/ActivityManager(515): at com.Android.server.am.ActivityManagerService.startActivityInPackage(ActivityManagerService.Java:3300)
03-26 14:22:35.893: W/ActivityManager(515): at com.Android.server.am.PendingIntentRecord.sendInner(PendingIntentRecord.Java:252)
03-26 14:22:35.893: W/ActivityManager(515): at com.Android.server.am.PendingIntentRecord.send(PendingIntentRecord.Java:192)
03-26 14:22:35.893: W/ActivityManager(515): at Android.content.IIntentSender$Stub.onTransact(IIntentSender.Java:64)
03-26 14:22:35.893: W/ActivityManager(515): at Android.os.Binder.execTransact(Binder.Java:404)
03-26 14:22:35.893: W/ActivityManager(515): at dalvik.system.NativeStart.run(Native Method)
Mein oben stehender Code funktioniert gut für alle Betriebssystemversionen außer KitKat 4.4 und 4.4. Aber ich habe die Lösung Ich habe den Empfänger in einen anderen Prozess gestellt und er funktioniert gut für alle Android-Betriebssystemversionen.
Wie so ..
aktivität Android: name = ". NotifyReciever" Android: process = ": remote"
und wir können hier mehr über Prozesse erfahren ....
Soll ich in meinem Receiver Android: process = ": remote" verwenden?
Dieses Problem wurde gemeldet, wenn KitKat 4.4 keine Aktivität öffnet, wenn Sie auf Benachrichtigung hier klicken, um eine Problem-URL zu erhalten
http://code.google.com/p/Android/issues/detail?id=63236
http://code.google.com/p/Android/issues/detail?id=61850
die empfohlene Problemumgehung besteht darin, vorhandene PendingIntent
abzubrechen oder PendingIntent.FLAG_CANCEL_CURRENT
zu verwenden.
ODER
Versuchen Sie es unten
Hinzufügen eines Flags in Activity
in AndroidManifiest.xml
Android:exported="true"
Dieses Problem ist für Android KitKat 4.4 und Lollipop ..__ bekannt. Bitte fügen Sie hinzu:
Android:exported="true"
in Android manifest innerhalb des Aktivitäts-Tags, das nach dem Tippen auf die Benachrichtigung in der Statusleiste geöffnet werden soll
Sie können auch PendingIntent.FLAG_ONE_SHOT
hinzufügen, um dies zu beheben.
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Fügen Sie im Manifest Android: exported = "true" hinzu
<activity
Android:name=".ListActivity"
Android:label="ListActivity"
Android:exported="true">
</activity>
ListActivity ist die Aktivität, die beim Benachrichtigungsklick geöffnet wird.
Wenn Android:exported="true"
für Sie nicht funktioniert, leiten Sie einfach die Absicht von Ihrer LAUNCHER-Aktivität um.
Prüfen Sie in Ihrer LAUNCHER-Aktivität wie folgt auf eingehende Benachrichtigungen:
if(getIntent().hasExtra("type") && getIntent().getStringExtra("type").compareTo("notification")==0){
Intent redirect = new Intent(getIntent());
redirect.setClass(this,YourNotificationActivity.class);
startActivity(redirect);
}
Wenn Sie die Absicht filtern müssen, verwenden Sie:
Intent incoming = getIntent();
if(intent.hasExtra("type") && intent.getStringExtra("type").compareTo("notification")==0){
Intent outgoing = new Intent(this, YourNotificationActivity.class);
outgoing.putExtra("title", incoming.getStringExtra("title");
outgoing.putExtra("content", incoming.getStringExtra("content");
startActivity(outgoing);
}
Versuchen Sie, dies zu ersetzen
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); // To open only one activity on launch.
PendingIntent pIntent = PendingIntent.getActivity(context, reqCode,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
zu
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); // To open only one activity on launch.
PendingIntent pIntent = PendingIntent.getActivity(context, reqCode, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
es wird für alle Versionen auch mit KitKat funktionieren.
Hinweis : Die Aktivität wird außerhalb des Kontexts einer vorhandenen Aktivität gestartet. Sie müssen daher das Intent.FLAG_ACTIVITY_NEW_TASK-Startflag im Intent verwenden.
Die von mass
vorgeschlagene Lösung hat sich für mich bewährt und ist sehr einfach, Sie müssen Ihren Code nicht ändern:
Fügen Sie Ihrem <activity>
-Tag in Ihrer AndroidManifest.xml das folgende Attribut hinzu:
Android:exported="true"
Es hat in einem Samsung Galaxy Young 2 mit Android 4.4.2 gearbeitet.
Ich hoffe es hilft dir.
long uniqueId = System.currentTimeMillis ();
/** This is the part to let your application recognise difference notification when the user click on the notification.
* You could use any unique string to represent your notification. But be sure each notification have difference action name.
**/
taskDetailIntent.setAction("notifi" + uniqueId);
PendingIntent contentIntent = PendingIntent.
getActivity(this, 0, taskDetailIntent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(contentIntent);
/** Set the unique id to let Notification Manager knows this is a another notification instead of same notification.
* If you use the same uniqueId for each notification, the Notification Manager will assume that is same notification
* and would replace the previous notification. **/
notificationManager.notify((int) uniqueId, builder.build());