web-dev-qa-db-fra.com

Notification Cliquez pas pour lancer l'activité donnée sur les téléphones Nexus

J'utilise ce code pour afficher la notification locale. Lorsque la notification arrive, cliquez sur Notification pour lancer ListActivity, mais sur le périphérique Google Nexus, la variable ListActiviy ne se lance pas lorsque vous cliquez sur Notification, mais sur d'autres périphériques, ce code fonctionne bien.

    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());

Ajout de logcat lorsque l'activité n'est pas lancée:

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)
21
Amrit

Mon code ci-dessus fonctionne bien pour toutes les versions de système d'exploitation, à l'exception de KitKat 4.4 et 4.4 + Mais j'ai la solution i, mettre le récepteur dans un autre processus et cela fonctionne bien pour la plupart des versions du système d'exploitation Android ...

Comme ça ..

activité Android: name = ". NotifyReciever" Android: process = ": à distance"

et nous pouvons en apprendre plus sur les processus ici ....

Devrais-je utiliser Android: process = ": remote" dans mon récepteur?

0
Amrit

Ce problème n'a pas été ouvert pour KitKat 4.4 lorsque vous cliquez sur la notification. L'URL est un problème.

http://code.google.com/p/Android/issues/detail?id=63236

http://code.google.com/p/Android/issues/detail?id=61850

la solution suggérée consiste à annuler la PendingIntent existante ou à utiliser PendingIntent.FLAG_CANCEL_CURRENT

OU 

Essayez ci-dessous

Ajout d'un drapeau dans Activity dans AndroidManifiest.xml

Android:exported="true"
65
Hardik

Ce problème est connu pour Android KitKat 4.4 et Lollipop . Veuillez ajouter:

Android:exported="true"

dans le manifeste Android à l'intérieur de la balise d'activité qui est censée être ouverte après avoir appuyé sur la notification de la barre d'état

10
satyapol

Vous pouvez également ajouter PendingIntent.FLAG_ONE_SHOT pour résoudre ce problème.

PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
3
Istvan Jonyer

Ajouter Android: exports = "true" dans le manifeste

<activity
            Android:name=".ListActivity"
            Android:label="ListActivity"
            Android:exported="true">
</activity>

ListActivity est l'activité qui sera ouverte lors du clic de notification.

1
manav patadia

Si Android:exported="true" ne fonctionne pas pour vous, redirigez simplement l'intention de votre activité LAUNCHER.

Dans votre activité LAUNCHER, vérifiez les notifications entrantes comme suit:

 if(getIntent().hasExtra("type") && getIntent().getStringExtra("type").compareTo("notification")==0){
        Intent redirect = new Intent(getIntent());
        redirect.setClass(this,YourNotificationActivity.class);
        startActivity(redirect);
 }

Si vous devez filtrer l'intention, utilisez:

 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);
 }
0
darkcoder

Essayez de remplacer ceci 

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);

à 

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);

cela fonctionnera pour toutes les versions, même avec KitKat également.

Remarque : L'activité sera démarrée en dehors du contexte d'une activité existante. Vous devez donc utiliser l'indicateur de lancement Intent.FLAG_ACTIVITY_NEW_TASK dans l'objectif. 

0
Sonali8890

La solution proposée par mass a fonctionné pour moi et c'est très simple, pas besoin de changer votre code:

Dans votre fichier AndroidManifest.xml, ajoutez l'attribut suivant à votre balise <activity>:

Android:exported="true" 

Il a fonctionné dans un Samsung Galaxy Young 2 avec Android 4.4.2.

0
david.perez

J'espère que cela vous aidera.

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());
0
palasha dbzgt