当应用程序在后台时,如何在点击通知时打开目标活动 [英] how opening the targeted activity on click of notification when app is in background

查看:142
本文介绍了当应用程序在后台时,如何在点击通知时打开目标活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用键值对通过 firebase 控制台发送通知,并在启动器活动中处理了通知.以下是尝试过的代码:

I have sent notification through firebase console using key vale pair and handled the notification in launcher activity. below is the tried code:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent.hasExtra("click_action")) {
        ClickActionHelper ck=new ClickActionHelper();
        ck.startActivity(intent.getStringExtra("click_action"), intent.getExtras(), this);
    }
}



 public class ClickActionHelper {
    public  void startActivity(String className, Bundle extras, Context context){
        Class cls=null;
        try {
            cls = Class.forName(className);
        }catch(ClassNotFoundException e){

        }
        Intent i = new Intent(context, cls);
        i.putExtras(extras);
        context.startActivity(i);
    }
}

但是这样我无法在点击通知时打开目标活动.有什么想法吗?

but this way i am not able to open the targeted activity on click of notification. Any ideas?

推荐答案

如果您需要导航任何 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 上的示例项目.

这篇关于当应用程序在后台时,如何在点击通知时打开目标活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆