且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当应用程序在后台时,如何在点击通知时打开目标活动

更新时间:2022-12-27 11:09:17

如果您需要导航任何 Activity,那么应该绑定 Notification class(NotificationCompat.Builder) ,它在实现中缺失.

If you need to navigate any Activity then there should be bind with Notification class(NotificationCompat.Builder) which is missing in implementation.

以下行对于重定向任何活动非常重要:

Below line is very important to redirect any Activity:

NotificationCompat.Builder builder = new **NotificationCompat.Builder(this); builder.setContentIntent(resultPendingIntent);**

这里 resultPendingIntent 用于包含 Activity 的后台堆栈,通知如下:

Here resultPendingIntent is used for to containing the backstack for the Activity with the notifications as below:

//获取一个包含整个返回栈 PendingIntent 的 PendingIntent

// Gets a PendingIntent containing the entire back stack PendingIntent

resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

完整的代码片段可在此处获得.

The complete code snippet is available here.

int id = 1;

Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());

  • 让我解释一下它是如何工作的
  • 假设您设置了来自服务器的响应,如下所示

  • Let me explain how it will works
  • Suppose you are set response from server as below

{"registration_ids": ["XXX", ...],数据": {id_offer":41"},通知": {"title": "这是标题","text": "你好,我是通知","icon": "ic_push","click_action": "ACTIVITY_XPTO"}}

{ "registration_ids": ["XXX", ...], "data": { "id_offer": "41" }, "notification": { "title": "This is the Title", "text": "Hello I'm a notification", "icon": "ic_push", "click_action": "ACTIVITY_XPTO" } }

并在您的清单文件中

<activity android:name=".ActivityXPTO" android:screenOrientation="sensor" android:windowSoftInputMode="stateHidden"> <intent-filter> <action android:name="ACTIVITY_XPTO" />
<category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>

当应用程序关闭或在后台并且用户点击通知时,它会打开我的 ActivityXPTO,以检索 id_offer 我只需要执行以下操作:

When the app is closed or in background and the user clicks on the notification it opens my ActivityXPTO, to retrieve the id_offer I only need to do:

public class ActivityXPTO extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
        String idOffer = "";
        Intent startingIntent = getIntent();
        if (startingIntent != null) {
            idOffer = startingIntent.getStringExtra("id_offer"); 
        // Retrieve the id
        }
        getOfferDetails(id_offer);
        }
    }
}

第二种方式

  • 通过如下所示的数据负载发送密钥,并通过 getIntent() 在 MainActivity 中获取密钥并调用特定的活动或片段.

  • Send key through data payload like below and get key in MainActivity via getIntent() and call specific activity or fragments.

json1.put("title","你的标题");

json1.put("title","Your Title");

json1.put("body","body content");

json1.put("body","body content");

json1.put("message","您的消息");

json1.put("message","Your Message");

json1.put("screen","2");//secondFragment 是导航抽屉中的第二个位置

json1.put("screen","2"); //secondFragment is 2nd position in nav drawer

json.put("data", json1);

json.put("data", json1);

GitHub 上的示例项目.