【问题标题】:how opening the targeted activity on click of notification when app is in background当应用程序在后台时,如何在点击通知时打开目标活动
【发布时间】:2016-10-19 06:39:54
【问题描述】:

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

@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);
    }
}

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

【问题讨论】:

  • @Raghunandan 我的问题不同,我需要启动一个特定的活动,这可以是应用程序中的任何活动。所以我需要为每个活动定义上述链接中给出的所有属性,如果我需要加载片段吗?
  • 我会为每个要打开的活动关联类型。基于此,我使用开关盒并导航到该特定活动。片段由活动托管。所以在这种情况下你需要启动一个片段
  • 我可以加载片段,但问题是我不确定应用程序在后台时应该使用哪个回调方法以及在哪个活动(mainActivity??)中......我已经尝试了上述解决方案但这对我不起作用....如果可能的话,请给我任何示例文件...谢谢
  • 再次阅读提到的链接。如果您正在寻找其他东西对不起我不知道

标签: android firebase notifications


【解决方案1】:

如果您需要导航任何 Activity,则应该与 Notification class(NotificationCompat.Builder) 绑定,这在实现中是缺失的。

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

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

这里resultPendingIntent用于包含Activity的backstack,通知如下:

// 获取一个包含整个后栈PendingIntent的PendingIntent

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

完整的代码sn-p可在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());

编辑:

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

    { "registration_ids": ["XXX", ...], “数据”: { “id_offer”:“41” }, “通知”: { "title": "这是标题", "text": "你好我是通知", “图标”:“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 我只需要这样做:

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 中的密钥并调用特定的活动或片段。

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

    json1.put("body","正文内容");

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

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

    json.put("数据", json1);

GitHub 上的示例项目。

【讨论】:

  • 根据上面的代码,当用户点击通知 ResultActivity.class 将会打开。但是在我的代码中,我尝试的是通过firebase控制台在key的值中发送类名:click_action。我想打开在 firebase 控制台中定义的特定类。这可能吗?
  • 我已在“编辑”部分下编辑了答案。请通过它。让我知道它是否对您有帮助。
【解决方案2】:

没有什么对我有用。对我有用的事情很简单。确保将其添加到要直接打开的活动中。

        <intent-filter>
            <action android:name="MainActivity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

并且您必须从推送通知中添加一个新的有效负载:click_action 它看起来像这样 -

"notification": {
  "title": "hello",
  "body": "test message",
  "click_action": "MAIN_ACTIVITY"
},

注意:您可以根据需要命名它MAIN_ACTIVITY,但两者必须相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多