Ale*_*lex 14 android firebase firebase-cloud-messaging firebase-notifications
我实施了Firebase并测试了Firebase通知.当应用程序在前台我没有问题时,我实现了一个扩展FirebaseMessagingService并处理onMessageReceived中的消息和数据的服务
当应用程序处于后台时,我遇到问题,我想发送一个通知,打开一个特定的活动并完成我计划做的事情,而不仅仅是打开应用程序.
我按照Firebase指南中的说明执行了操作,但我无法启动特定活动.
这里的清单:
<activity android:name=".BasicNotificationActivity">
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
这里是Firebase控制台.我必须在这些字段中编写什么来打开我的"BasicNotificationActivity"?
Mic*_* F. 22
这与此问题重复:Firebase FCM通知click_action有效内容
但是,这个问题的作者接受的答案只是表明使用Firebase控制台是不可能的,但它是 - 通过简单的解决方法.diidu对同一个问题的回答解释了我将使用的解决方法.
更新:
详细说明他的答案:
添加辅助类(或startActivity()以某种方式实现方法):
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);
}
}
Run Code Online (Sandbox Code Playgroud)
在你的应用程序的启动器活动中,调用mehtod来检查任何新的意图onCreate()和onNewIntent()(onNewIntent()仅调用而不是onCreate()如果使用单顶标志启动活动):
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
并在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);
}
}
Run Code Online (Sandbox Code Playgroud)
要使用firebase控制台发送通知,请将键值对作为自定义数据放置,如下所示:
Key: click_action
Value: <fully qualified classname of your activity>
Run Code Online (Sandbox Code Playgroud)
现在,当收到并点击通知时,它将打开您的活动.如果您的应用程序位于前台,它也会立即更改为活动 - 询问用户是否要参加此活动(通过显示对话框onMessageReceived())可能会很好.
这个问题是 2 岁。但是还是有关系的。这就是您可以使用 firebase 控制台(没有“click_action”)执行此操作的方法。
当您的应用程序在后台onMessageReceived时不会被调用。但是,当您在应用程序处于后台时收到通知时,您将收到Intent您在 firebase 控制台中指定的自定义数据。
因此,当用户点击通知时,the intent将执行通知以打开您的启动器活动。您可以getIntent().hasExtra("key")在启动器活动中检查数据。哪里"key"是您在控制台中指定的任何键。
检查你是否有那个"key",你可以Intent再打一个电话startActivity
这是我的一个实现,
在我的SplashActivity> OnCreate(如果你有一个启动画面作为启动器活动,这个方法看起来最好):
if (getIntent().hasExtra("key")){
Intent intent = new Intent(this, TargetActivity.class);
startActivity(intent);
finish();
} else {
startActivity(new Intent(this, MainActivity.class));
finish();
}
Run Code Online (Sandbox Code Playgroud)
这将启动TargetActivity. 您可以根据需要为此添加任何功能:)
| 归档时间: |
|
| 查看次数: |
25156 次 |
| 最近记录: |