2017-04-12 12 views
2

Ich habe ein Problem mit FCM, wenn ich die Benachrichtigung erhalte, ich möchte es spezifische Aktivität öffnet, standardmäßig, wenn ich nicht hinzufügen click_action es öffnet die Hauptaktivität der App, aber wann ich füge click_action und klicken sie auf die Benachrichtigung, um sie keine Aktion durchführen.FCM Benachrichtigung click_action nicht funktioniert

Hier ist die JSON ich im Web-Service verwenden:

{ 
    "registration_ids": [ 
     "f4............LL" 
    ], 
    "notification": { 
     "title": "Rebate Confirmation", 
     "text": "Please Confirm", 
     "sound": "default", 
     "click_action": ".Activities.CustomerRebateConfirmation" 
    }, 
    "data": { 
     "merchant_id": "20", 
     "customer_id": "1", 
     "points": "10", 
     "totalpoints": "100", 
     "message": "Please Confirm", 
     "type": "customer_points_rebate_confirmation" 
    } 
} 

und das ist meine onMessageReceived Methode:

public void onMessageReceived(RemoteMessage remoteMessage) { 

     Log.e(TAG, "From: " + remoteMessage.getFrom()); 
     customerRebateDetails = new String[5]; 

     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 
      Log.e(TAG, "Message data payload: " + remoteMessage.getData()); 
      Log.e(TAG, "Message notification: " + remoteMessage.getNotification().getBody()); 

      String type = remoteMessage.getData().get("type"); 
      String message = remoteMessage.getData().get("message"); 
      String text = remoteMessage.getNotification().getBody(); 
      Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

      switch (type){ 
       case "customer_points_rebate_confirmation": 
        customerRebateDetails[0] = remoteMessage.getData().get("customer_id"); 
        customerRebateDetails[1] = remoteMessage.getData().get("merchant_id"); 
        customerRebateDetails[2] = remoteMessage.getData().get("points"); 
        customerRebateDetails[3] = remoteMessage.getData().get("totalpoints"); 
        Intent customerRebate = new Intent(this, CustomerRebateConfirmation.class); 
        customerRebate.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        customerRebate.putExtra("customer_points_rebate_confirmation", customerRebateDetails); 
        PendingIntent customerRebatePendingIntent = PendingIntent.getActivity(this, 0, customerRebate, 
          PendingIntent.FLAG_ONE_SHOT); 
        NotificationCompat.Builder customerRebateBuilder = new NotificationCompat.Builder(this) 
          .setSmallIcon(R.mipmap.ic_launcher) 
          .setContentTitle(message) 
          .setContentText(text) 
          .setSound(defaultSoundUri) 
          .setAutoCancel(true) 
          .setContentIntent(customerRebatePendingIntent); 
        NotificationManager customerRebateManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
        customerRebateManager.notify(0, customerRebateBuilder.build()); 
        break; 
      } 

Wer weiß, was das Problem der Umsetzung?

Beachten Sie, dass es funktioniert gut, wenn der App im Vordergrund, aber es funktioniert nicht, wenn die Anwendung im Hintergrund ist.

+0

ersetzen können Sie Ihre Frage mit Benachrichtigungscode aktualisieren? – pRaNaY

+0

Sie den PHP-Code bedeuten? – Badr

+0

Nein. Code, bei dem Sie eine Benachrichtigung aus der FCM-Nachricht generieren – pRaNaY

Antwort

2

Achten Sie darauf, diese Zeilen in Manifest-Datei in Ihrem CustomerRebateConfirmation Aktivität hinzugefügt haben ...

<intent-filter> 
    <action android:name=".Activities.CustomerRebateConfirmation" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 
+0

genau! Das war der fehlende Teil – Badr

+0

Groß .. !!! hoffe du lernst und lernst mehr .... :) –

2

versuchen, dies in Ihrer MainActivity Klasse.

@Override 
public void onNewIntent(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     if (extras.containsKey("type")) { 
      String type = extras.getString("type"); 
      if (type.equals("test type")) { 
       Toast.makeText(this, extras.getString("message"), Toast.LENGTH_SHORT).show(); 
      } 

     } 
    } 
} 

Ich habe dies versucht, wenn ich von diesem Problem betroffen bin. Hoffe, das wird dir hilfreich sein.

MessagingServiceClass.java

public class MyFirebaseMessagingService extends FirebaseMessagingService { 
private static final String TAG = "FCM Service"; 
int mNoti = 2019; 
private final int NOTIFICATION_ID = 237; 
private static int value = 0; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    // TODO: Handle FCM messages here. 
    String title = "0"; 
    String type = "0"; 
    String message = "0"; 
    if (remoteMessage.getData().size() > 0) { 
     type = remoteMessage.getData().get("type"); 
     title = remoteMessage.getData().get("title"); 
     message = remoteMessage.getData().get("message"); 
     sendNotification(type, title, message); 
    } 

} 

private void sendNotification(String type, String title, String message) { 
    Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(), 
      R.mipmap.ic_launcher); 

    //AppMethod.setIntegerPreference(getApplicationContext(),NOTIFICATION_ID,) 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.putExtra("type", type); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 


    PendingIntent pendingIntent = PendingIntent.getActivity(this, value, intent, PendingIntent.FLAG_ONE_SHOT); 
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setLargeIcon(icon) 
      .setContentTitle(title) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(value, notificationBuilder.build()); 
    value++; 
} 

}

0

In Ihrem addFlags, versuchen Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK hinzuzufügen. Für mich geht das.

1

Ort Intent-Filter in Ihrem mainfiest innerhalb Aktivitäts-Tag-Hexe möchten Sie klicken Leistung in Aktion verwenden Sie den gleichen Namen zu schreiben, den Sie in Payload-Freunden gegeben haben.

 <activity name="your activity name" > 
     <intent-filter> 
      <action android:name=".Activities.CustomerRebateConfirmation" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
     <activity> 
0

in onMessageReceived()

String click_action = remoteMessage.getNotification().getClickAction(); 

erstellen und Ihre Absicht, mit diesem

Intent customerRebate = new Intent(click_action); 
Verwandte Themen