从Firebase通知打开特定活动

10

我将 Firebase 通知集成到我的应用程序中,但我希望发送一条通知,打开特定的活动并执行我安排要执行的操作,而不仅仅是打开应用程序。就像一条通知,当用户单击它时将推送用户访问 Google Play 商店。

我看到一个代码 Firebase 控制台:如何为通知指定 click_action,我使用了它,但出现了初始化变量 cls 的错误。我尝试通过定义 cls=null 来解决问题,以清除错误,但它无法使用 click_action 打开我指定的活动。

public class ClickActionHelper { 
    public static void startActivity(String className, Bundle extras, Context context){ 
        Class cls=null; 
        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); 
    }     
}

请问我哪里做错了?我该如何让这个工作起来呢?

3个回答

22

如果想在后台打开应用并执行特定操作,请在通知有效负载中设置 click_action,并将其映射到活动中的意图过滤器以启动所需的 Activity。例如,将 click_action 设置为 OPEN_ACTIVITY_1,以触发以下类似的意图过滤器:

按照 FCM 文档建议,请求后端以如下形式发送 JSON 数据:

{
    "to": "some_device_token",

    "content_available": true,
    "notification": {
        "title": "hello",
        "body": "yo",
        "click_action": "OPEN_ACTIVITY_1" // for intent filter in your activity
    },

    "data": {
        "extra": "juice"
    }
}

在您的清单文件中添加以下内容,为您的活动添加意图过滤器:

并在您的清单文件中为您的活动添加以下intent-filter:

<intent-filter>
  <action android:name="OPEN_ACTIVITY_1" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
当您点击通知时,它会打开应用并直接跳转到您在click_action中定义的活动,此处为"OPEN_ACTIVTY_1"。在该活动内,您可以通过以下方式获取数据:

当您点击通知时,它将打开应用并直接跳转到您在click_action中定义的活动,在本例中为“OPEN_ACTIVTY_1”。然后,在该活动中,您可以通过以下方式获取数据:

Bundle b = getIntent().getExtras();// add these lines of code to get data from notification
String someData = b.getString("someData");

查看以下链接以获取更多帮助:

Firebase FCM通知点击操作负载

当应用程序处于后台时,Firebase onMessageReceived未被调用

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


1

我知道这个问题已经存在一段时间了,但我仍然想展示我的解决方案。它比那些提出的方案要简单得多。现在我想知道它是否是不好的实践。但它有效:我使用负载JSON对象来存储一个整数:

JSONObject payload = data.getJSONObject("payload");
int destination = payload.getInt("click_action");

然后我只需使用简单的switch语句,根据整数结果启动正确的活动:
Intent resultIntent;
switch(destination){
    case 1:
        resultIntent = new Intent(getApplicationContext(), UserProfileActivity.class);
        resultIntent.putExtra("message", message);
        break;
    default:
        resultIntent = new Intent(getApplicationContext(), MainActivity.class);
        resultIntent.putExtra("message", message);
        break;
}

简单。

0
<activity android:name=".design.NotificationActivity"
        android:launchMode="singleTask"
        android:taskAffinity=""
        android:excludeFromRecents="true"
        android:parentActivityName=".MainActivity"
        >
        <intent-filter>
            <action android:name="NotificationActivity" />
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>

    </activity>

        $ url = "https://fcm.googleapis.com/fcm/send";
        $token = "";
        $serverKey = '';
        $title = "your title";
        $body = "mesaage";
        $notification = array('title' =>$title ,
        'body' => $body, 
        'sound' => 'default',
        'badge' => '1',
        'click_action' => 'NotificationActivity'
        );
        $arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
        $json = json_encode($arrayToSend);
        $headers = array();
        $headers[] = 'Content-Type: application/json';
        $headers[] = 'Authorization: key='. $serverKey;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
        //Send the request
        $response = curl_exec($ ch);
        //Close request
        if ($response === FALSE) {
        die('FCM Send Error: ' . curl_error($ch));
        }
        curl_close($ch)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接