Firebase控制台:如何为通知指定click_action [英] Firebase console: How to specify click_action for notifications

查看:1501
本文介绍了Firebase控制台:如何为通知指定click_action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施了Firebase并测试了Firebase通知。
当应用处于前台时,我没有问题,我实现了一个扩展 FirebaseMessagingService 的服务,并处理 onMessageReceived

中的消息和数据>

当应用程序处于后台时,我遇到了问题,我想发送一个通知,打开一个特定的活动,并执行我计划要做的事情,而不仅仅是打开应用程序。 p>

我按照Firebase指南中的描述进行了操作,但无法启动具体的活动。



清单:

 < activity android:name =。BasicNotificationActivity> 
< intent-filter>

Firebase FCM通知click_action有效载荷



但是,这个问题的作者所接受的答案只是说,这是不可能与Firebase控制台,但它是 - 一个简单的解决方法。 diidu 对同一问题的回答解释了我将使用的解决方法。



更新:

详细解释他的回答:

添加一个辅助类(或者实现 startActivity()方法):

  public class ClickActionHelper {
public static void startActivity(String className,Bundle extras,Context context){
Class cls;
尝试{
cls = Class.forName(className);
} catch(ClassNotFoundException e){
//表示您在Firebase控制台中输入了错误的输入
}
Intent i = new Intent(context,cls);
i.putExtras(extras);
context.startActivity(i);






在应用程序的启动器活动中,调用()方法检查 onCreate() onNewIntent() onNewIntent() 只会被调用,而不是 onCreate(),如果Activity是以单顶标志启动的话):

  @Override 
保护无效的onCreate(捆绑包){
[...]
checkIntent(getIntent());


@Override
保护无效的onNewIntent(意图意图){
super.onNewIntent(intent);
[...]
checkIntent(intent);

$ b $ public void checkIntent(Intent intent){
if(intent.hasExtra(click_action)){
ClickActionHelper.startActivity(intent.getStringExtra( click_action),intent.getExtras(),this);




$ onMessageReceived()


$ b $ pre $ < data = remoteMessage.getData();
if(data.containsKey(click_action)){
ClickActionHelper.startActivity(data.get(click_action),null,this);




$ b要使用Firebase控制台发送通知,值为
$ b

 键:click_action 
值:<您的完全限定的类名活性GT;

现在,当收到通知并点击通知后,就会打开您的活动。如果您的应用程序处于前台,它也会立即转换为活动 - 向用户询问他是否想要参加此活动可能会很好(通过在 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.

I did as described on the Firebase guide, but I'm not able to start the specific activity.

Here the manifest:

<activity android:name=".BasicNotificationActivity">
            <intent-filter>
                <action android:name="OPEN_ACTIVITY_1" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

And here the Firebase Console. What do I have to write in those fields to open my "BasicNotificationActivity"?

解决方案

This is a duplicate of this question: Firebase FCM notifications click_action payload

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:

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

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

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

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>

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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