这是这个问题的重复:Firebase FCM 通知 click_action 有效负载 https://stackoverflow.com/q/37407366/5433102
但是这个问题的作者接受的答案只是表明 Firebase Console 不可能,但它是 - 有一个简单的解决方法。此回答来自 diidu https://stackoverflow.com/a/37836984/5433102对同一个问题解释了我将使用的解决方法。
UPDATE:
详细说明他的答案:
添加一个辅助类(或实现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);
}
}
在应用程序的启动器活动中,调用一个方法来检查中的任何新意图onCreate()
and onNewIntent()
(onNewIntent()
仅被调用而不是onCreate()
如果 Activity 使用单顶标志启动):
@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);
}
}
And in 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 控制台发送通知,请将键值对作为自定义数据,如下所示:
Key: click_action
Value: <fully qualified classname of your activity>
现在,当收到通知并单击时,它将打开您的活动。如果您的应用程序位于前台,它也会立即更改为该活动 - 最好询问用户是否想要进入该活动(通过在onMessageReceived()
).