2017-04-12 182 views
2

我有一个FCM问题,当我收到通知时,我希望它打开特定的活动,默认情况下我不添加click_action它打开应用程序的主要活动,但是当我添加click_action并点击通知它不执行任何操作。FCM通知click_action不起作用

这是我在Web服务中使用JSON:

{ 
    "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" 
    } 
} 

,这是我onMessageReceived方法:

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

有谁知道什么是执行的问题?

请注意,它在应用程序处于前景时工作正常,但在应用程序处于后台时不起作用。

+0

你可以更新通知码你的问题? – pRaNaY

+0

你的意思是php代码? – Badr

+0

不是。从FCM消息生成通知的代码。 – pRaNaY

回答

2

确保您已在清单文件的CustomerRebateConfirmation活动添加了此行......在你的mainfiest活动标签女巫内

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

到底!这是缺少的部分 – Badr

+0

大..!希望你学习和学习更多。 :) –

2

在您的MainActivity类中尝试此操作。

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

     } 
    } 
} 

我已经试过这个,当我受到这个问题。希望这会对你有所帮助。

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

在你addFlags,试加Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK。这个对我有用。

1

地方意图过滤器要使用点击性能在行动你有写出你在有效载荷恶魔中给出的同名。

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

在onMessageReceived()创建

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

并替换你的意图与此

Intent customerRebate = new Intent(click_action);