Firebase控制台:如何为通知指定click_action

时间:2016-06-29 22:54:01

标签: android firebase firebase-cloud-messaging firebase-notifications

我实施了Firebase并测试了Firebase通知。 当应用程序位于前台时,我没有遇到任何问题,我实施了一项服务,该服务扩展了 FirebaseMessagingService 并处理 onMessageReceived

中的消息和数据

我在应用程序处于后台时遇到问题,我想发送一个通知,打开一个特定的活动,按照我的计划做,而不仅仅是打开应用程序。

我按照Firebase指南中的说明操作,但我无法启动特定活动。

这里的清单:

<activity android:name=".BasicNotificationActivity">
            <intent-filter>
                <action android:name="OPEN_ACTIVITY_1" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

这里是Firebase控制台。我需要在这些字段中编写什么来打开我的&#34; BasicNotificationActivity&#34;?

Firebase console

4 个答案:

答案 0 :(得分:22)

这是此问题的副本:Firebase FCM notifications click_action payload

但是,这个问题的作者接受的答案只是表明使用Firebase控制台是不可能的,但它是 - 通过简单的解决方法。 This answer by diidu对同一个问题解释了我将使用的解决方法。

<强>更新
详细说明他的答案:

以某种方式添加辅助类(或实现@card.exp_year = 2026方法):

startActivity()

在您的应用的启动器活动中,调用mehtod检查public class ClickActionHelper { public static void startActivity(String className, Bundle extras, Context context){ Class cls; try { cls = Class.forName(className); }catch(ClassNotFoundException e){ //means you made a wrong input in firebase console } Intent i = new Intent(context, cls); i.putExtras(extras); context.startActivity(i); } } onCreate()中的任何新意图(onNewIntent()仅调用onNewIntent()而不是onCreate()是用单顶旗启动的:)

@Override
protected void onCreate(Bundle bundle) {
    [...]
    checkIntent(getIntent());
 }

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    [...]
    checkIntent(intent);
}

public void checkIntent(Intent intent) {
       if (intent.hasExtra("click_action")) {
        ClickActionHelper.startActivity(intent.getStringExtra("click_action"), intent.getExtras(), this);
       }
}

onMessageReceived()

 public void onMessageReceived(RemoteMessage remoteMessage) {
     Map<String, String> data = remoteMessage.getData();        
     if (data.containsKey("click_action")) {
         ClickActionHelper.startActivity(data.get("click_action"), null, this);
     }
 }

要使用firebase控制台发送通知,请将键值对作为自定义数据放置,如下所示:

Key: click_action
Value: <fully qualified classname of your activity>

现在收到并点击通知时,它会打开您的活动。如果您的应用程序位于前台,它也会立即更改为活动 - 询问用户是否要参加此活动可能会很好(通过在onMessageReceived()中显示对话框)。

答案 1 :(得分:1)

我已使用handleIntent(Intent intent)方法处理intent并移至该特定屏幕。 下面是我的代码,即使应用程序处于后台也能正常运行:

FCMMessagingService.java extends FCMMessagingService

@Override
    public void handleIntent(Intent intent) {
        super.handleIntent(intent);

     Intent i = null;
     String value_action = "";

      if (intent.getExtras() != null) {
          if (key.equals("click_action")) {
                value_action = intent.getExtras().getString(key);
            }

          i = new Intent(FCMMessagingService.this, MainActivity.class);
          i.putExtra("notificationFlag", value_action);
          i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

      }
     PendingIntent pendingIntent = PendingIntent.getActivity(this, notifCount, i, PendingIntent.FLAG_ONE_SHOT);
        final int icon = R.mipmap.logo;
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);

    Notification notification;
    notification = notificationBuilder.setSmallIcon(icon).setTicker(title)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentText(body)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.mipmap.notification_icon)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), icon))
            .build();


    notificationBuilder.setContentTitle(title);
    notificationBuilder.setContentText(body);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSound(defaultSoundUri);
    notificationBuilder.setContentIntent(pendingIntent);
    notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), icon));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notificationBuilder.setSmallIcon(R.mipmap.logo);
    } else {
        notificationBuilder.setSmallIcon(R.mipmap.logo);
    }

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notifCount, notification);

}

答案 2 :(得分:1)

这个问题已经2岁了。但是仍然有意义。 这就是您可以使用Firebase控制台(不使用click_action)进行的方式。

您的应用在后台onMessageReceived不会被调用。但是,当您的应用程序在后台运行时收到通知时,您将获得Intent以及在Firebase控制台中指定的自定义数据。

因此,当用户点击通知the intent时,将执行以打开您的启动器活动。您可以在启动器活动中通过getIntent().hasExtra("key")检查数据。 "key"是您在控制台中指定的任何键。

检查您是否有"key",然后可以再创建一个Intent并致电startActivity

这是我的实现,

在我的SplashActivity> OnCreate上(如果您将启动画面作为启动器活动,则此方法看起来最好):

if (getIntent().hasExtra("key")){

      Intent intent = new Intent(this, TargetActivity.class);
      startActivity(intent);
      finish();

} else {
      startActivity(new Intent(this, MainActivity.class));
      finish();
}

这只会启动TargetActivity。您可以根据需要添加任何功能:)

答案 3 :(得分:0)

我也有像你这样的麻烦,但我没有得到任何正确答案 这就是我所知道的事实,

  
      
  1. onNewIntent
  2.   

当应用为背景时,此覆盖方法无法正常工作默认操作 android.intent.action.MAIN

  
      
  1. Firebase控制台
  2.   

firebase控制台无法处理 click_action ,这意味着点击操作无法在应用程序处于后台时触及应用程序。

  
      
  1. CURL
  2.   

Curl正在app app中工作但不是前景可能是我无法找到的。

curl --header "Authorization: key=<Colud_Messaging_Server_Key_here>" --header Content-Type:"application/json" https://fcm.googleapis.com/fcm/send  -d "{\"to\":\"dwdfosu6SDs:APA91bFWxZZB2f8AOgP9MG504cy5AhFECHGbYFfIzVixoAfy-ErT0NYQrREg150CbKYBtk3Ywpu7WQuWsQhk-VmoOe-0iDK3ZgaFZNvYxDuixZB8zQp0zydoXhyrMosi5C4eyBb7Ai1z\",\"notification\": {\"title\": \"Click Action Message\",\"text\": \"Sample message\",\"click_action\":\"noti\",\"ticket_download\":\"noti\"}}"

添加Minifest

<activity
   android:name=".activity.TicketDownload"
   android:exported="true">
   <intent-filter>
      <action android:name="noti" />
      <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
</activity>

注意:cloud_messaging_server_key是定位设置&gt; cloudmessaging

如果有任何新的事实,请告诉我。