我实现了 Firebase 并测试了 Firebase 通知.当应用程序在前台时我没有问题,我实现了一个扩展 FirebaseMessagingService 的服务并处理 onMessageReceived
I implemented Firebase and testing the Firebase notifications. When the app is in the foreground I don't have problems, I implemented a service that extends FirebaseMessagingService and handle the message and data in onMessageReceived
当应用程序处于后台时我遇到问题,我想发送一个通知以打开特定活动并执行我计划执行的操作,而不仅仅是打开应用程序.
I have problems when the app is in background, I would like to send a notification that opens a specific activity and does what I schedule to do, not just opening the App.
我按照 Firebase 指南中的说明进行操作,但无法启动特定活动.
I did as described on the Firebase guide, but I'm not able to start the specific activity.
这里是清单:
<activity android:name=".BasicNotificationActivity">
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
这里是 Firebase 控制台.我必须在这些字段中写什么才能打开我的BasicNotificationActivity"?
And here the Firebase Console. What do I have to write in those fields to open my "BasicNotificationActivity"?
这是这个问题的重复:Firebase FCM 通知 click_action 有效负载
但是这个问题的作者接受的答案只是表明 Firebase 控制台是不可能的,但它是 - 有一个简单的解决方法.这个 diidu 的回答对同一问题的解释解释了我将使用的解决方法.
But the answer that was accepted by the author of this question just states that it is not possible with Firebase Console, but it is - with an easy workaround. This answer by diidu to the same question explains the workaround I would use.
更新:
详细说明他的答案:
UPDATE:
To elaborate on his answer:
添加一个辅助类(或以某种方式实现 startActivity()
方法):
Add a helper class (or implement startActivity()
method somehow):
public class ClickActionHelper {
public static void startActivity(String className, Bundle extras, Context context){
Class cls;
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);
}
}
在应用的启动器活动中,调用一个方法来检查 onCreate()
和 onNewIntent()
中的任何新意图 (onNewIntent()
仅在使用单顶标志启动 Activity 时才调用而不是 onCreate()
):
In the launcher-activity of your app, call a mehtod to check any new intents in onCreate()
and onNewIntent()
(onNewIntent()
is only called instead of onCreate()
if Activity is launched with single-top flag):
@Override
protected void onCreate(Bundle bundle) {
[...]
checkIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
[...]
checkIntent(intent);
}
public void checkIntent(Intent intent) {
if (intent.hasExtra("click_action")) {
ClickActionHelper.startActivity(intent.getStringExtra("click_action"), intent.getExtras(), this);
}
}
而在onMessageReceived()
中:
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
if (data.containsKey("click_action")) {
ClickActionHelper.startActivity(data.get("click_action"), null, this);
}
}
要使用 firebase 控制台发送通知,请将键值对作为自定义数据,如下所示:
To send a notification with firebase console, put a key-value-pair as custom data like this:
Key: click_action
Value: <fully qualified classname of your activity>
现在,当收到并点击通知时,它将打开您的活动.如果您的应用程序在前台,它也会立即更改为活动 - 询问用户是否想进入此活动可能会很好(通过在 onMessageReceived() 中显示一个对话框
).
Now when a notification is received and clicked on, it will open your activity. If your app is in foreground, it will also immediately change to the activity - it would probably be good to ask the user if he wants to go to this activity or not (by showing a dialog in onMessageReceived()
).
这篇关于Firebase 控制台:如何为通知指定 click_action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!