Antwort

5

Im Nachrichtenempfänger, ich folgendes: Aktivität

final Intent notificationIntent = new Intent(context, YourActivity.class); 
notificationIntent.setAction(Intent.ACTION_MAIN); 
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); 

Hier ersetzen YourActivity.class mit dem Eintrag. Das hat für mich funktioniert.

+2

Was ist, wenn die Anwendung bereits ausgeführt wird? – Ahmed

+0

Was passiert, wenn die App aus der Aufgabenliste entfernt wird? In diesem Fall wird "Nachrichtenempfänger" nicht angerufen. Es wird nach System gehandhabt und es wurde mit dem Tag "click_action" geklickt. Aber meine Sorge ist offen meine Zielaktivität nach der Benachrichtigung angekommen. –

+0

Wie bekomme ich hier Kontext in fcm Nachricht ?? –

2

können Sie NotificationManager verwenden, um Ihre Aktivität zu starten.

versuchen Im Folgenden Code in Ihrer onMessage() Methode zu verwenden, die überschriebene Methode in der Klasse ist, die GCMBaseIntentService Klasse von GCM erstreckt.

int icon = R.drawable.your_app_icon; 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, message, when); 
     String title = context.getString(R.string.app_name); 

     Intent notificationIntent = new Intent(context, YOUR_ACTIVITY.class); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     // notificationIntent.putExtra("PostName", vPostText); 
     // Log.i(TAG, "Sent Postid " + postid); 

     // Util util = (Util) context.getApplicationContext(); 
     // util.setPostID(postid); 
     // util.setNotify(true); 
     // util.setUserNAME(vPortCode); 
     // util.setPostNAME(vPostText); 
     // util.setmEDIA(vMedia); 
     // util.setmEDIATHHUMB(vMediaThumb); 
     // util.setmEDIATYPE(vMediaType); 
     // util.setAirportName(vAirportName); 

     notificationIntent.putExtra("Set_image", true); 
     notificationIntent.putExtra("Notify", true); 

     // set intent so it does not start a new activity 
     // notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     // notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     // notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
     // | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = PendingIntent.getActivity(context, 
       (int) System.nanoTime(), notificationIntent, 0); 

     notification.setLatestEventInfo(context, title, message, intent); 
     notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify((int) System.nanoTime(), notification); 
+1

Vielen Dank.Ich weiß, wie Sie eine Aktivität aus der Anwendung starten. Aber, was ich tun möchte, ist, wenn meine Anwendung im Hintergrund ist, würde ich es in den Vordergrund, bringen, wenn es im Vordergrund ist, dann würde ich die Verarbeitung fortsetzen, und wenn meine Anwendung in ist der stop/killed state dann möchte ich "relaunch die anwendung" nicht aktivität. Also können Sie mir sagen, ob es eine Möglichkeit gibt, die Anwendung zu starten, wenn sich die Anwendung im Status Stop/Getötet bei Benachrichtigung befindet? – jasdmystery

+0

@jasdmystery: Hast du etwas herausgefunden? Ich habe dasselbe Problem. – user836026

+1

Siehe die Antwort oben. – jasdmystery

Verwandte Themen