Android activity 点击Firebase通知时显示特定活动

Android activity 点击Firebase通知时显示特定活动,android-activity,firebase,firebase-cloud-messaging,firebase-notifications,Android Activity,Firebase,Firebase Cloud Messaging,Firebase Notifications,当用户点击Firebase控制台发送的通知时,我需要启动特定的Android活动,只有当用户点击通知时。如何在以下两种情况下实现这一点: 应用未打开或应用正在后台运行 应用程序位于前台 你可以通过两种方式实现这一点 1。第一条路 在通知有效负载中添加单击操作, jNotification.put(“单击操作”、“打开活动1”) private void pushNotification(String token) { JSONObject jPayload = new JSONO

当用户点击Firebase控制台发送的通知时,我需要启动特定的Android活动,只有当用户点击通知时。如何在以下两种情况下实现这一点:

  • 应用未打开或应用正在后台运行
  • 应用程序位于前台

  • 你可以通过两种方式实现这一点

    1。第一条路

    在通知有效负载中添加单击操作, jNotification.put(“单击操作”、“打开活动1”)

    private void pushNotification(String token) {
            JSONObject jPayload = new JSONObject();
            JSONObject jNotification = new JSONObject();
            JSONObject jData = new JSONObject();
            try {
                jNotification.put("title", "Google I/O 2016");
                jNotification.put("text", "Firebase Cloud Messaging (App)");
                jNotification.put("sound", "default");
                jNotification.put("badge", "1");
                jNotification.put("click_action", "OPEN_ACTIVITY_1");
    
                jData.put("picture_url", "http://opsbug.com/static/google-io.jpg");
    
                jPayload.put("to", token);
                //jPayload.put("to", "/topics/news");
                //jPayload.put("condition", "'logined' in topics || 'news' in topics");
                //jPayload.put("registration_ids", jData);
                jPayload.put("priority", "high");
                jPayload.put("notification", jNotification);
                jPayload.put("data", jData);
    
                URL url = new URL("https://fcm.googleapis.com/fcm/send");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Authorization", AUTH_KEY);
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setDoOutput(true);
    
                // Send FCM message content.
                OutputStream outputStream = conn.getOutputStream();
                outputStream.write(jPayload.toString().getBytes());
    
                // Read FCM response.
                InputStream inputStream = conn.getInputStream();
                final String resp = convertStreamToString(inputStream);
    
                Handler h = new Handler(Looper.getMainLooper());
                h.post(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("Response", resp);
                        //txtStatus.setText(resp);
                    }
                });
            } catch (JSONException | IOException e) {
                e.printStackTrace();
            }
        }
    
    将其添加到您的AndroidManifest.xml中

            <activity android:name="com.example.fcm.SecondActivity">
                <intent-filter>
                    <action android:name="OPEN_ACTIVITY_1" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    
    2。第二条路

    通过下面的数据有效负载发送密钥,通过getIntent()获取MainActivity中的密钥,并调用特定的活动或片段

            json1.put("title","Your Title");  
            json1.put("body","body content");  
            json1.put("message","Your Message");  
            json1.put("screen","2");   //secondFragment is 2nd position in nav drawer
            json.put("data", json1); 
    
    MainActivity.java

    public class SecondActivity extends AppCompatActivity {
        private static final String TAG = "SecondActivity";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            Bundle bundle = getIntent().getExtras();
    
            if (bundle != null) {
                for (String key : bundle.keySet()) {
                    Object value = bundle.get(key);
                    Log.d(TAG, "Key: " + key + " Value: " + value.toString());
                }
            }
        }
    }
    
    Intent intent = getIntent();
            String pos = getIntent().getStringExtra("screen");
            if(pos !=null){
                selectDrawerItem(navigationView.getMenu().getItem(Integer.parseInt(pos)));
            }
    
    GITHUB中的示例项目,

    当前无法控制点击通知时启动的活动。请看Hi Frank,您是否也可以告诉我如何从firebase以json的形式发送消息/通知。您需要从服务器发送下游消息。请参阅@FrankvanPuffelen感谢您的及时回复。如何从Firebase控制台或任何第三方云推送平台发送带有数据(json)的下游消息,因为我没有访问任何内部服务器的权限。在Firebase composer的高级部分,您将能够添加作为intent extras传递到启动活动的键值对。
    Intent intent = getIntent();
            String pos = getIntent().getStringExtra("screen");
            if(pos !=null){
                selectDrawerItem(navigationView.getMenu().getItem(Integer.parseInt(pos)));
            }