如何在收到推送通知后触发一个动作

huangapple go评论66阅读模式
英文:

How to trigger an action once the push notification is received

问题

我想使用Java构建一个Android应用程序。我将使用GCM来发送推送通知。然而,我不确定是否可以将推送通知用作触发器。因此,我想要做类似于这样的事情:

如果收到来自另一个设备的推送通知,
将一个布尔值设置为true,
使用这个布尔值并触发一个操作。

我只是想找出是否有可能。我还没有开始编码。谢谢。

英文:

I want to build an android app using java. I will use GCM to send push notifications. However, I am not sure if is it possible that we can use push notifications as a trigger. So I want to do something like this:

If a push notification is received from another device,
Make a boolean true,
Use this boolean and trigger an action.

I just want to find out if it is possible. I haven't start to code yet. Thank you.

答案1

得分: -1

是的,您应该使用 FCM,因为Google 已经弃用了 GCM

通过使用 FCM,您可以接收通知并在设备上触发操作。

您需要在清单文件中注册一个服务:

<service
        android:name=".MyFirebaseMessagingService"
        android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

然后创建一个扩展了 FirebaseMessagingService 并重写了 onMessageReceived 方法的 MyFirebaseMessagingService 类:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // 检查消息是否包含数据有效负载。
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    // 检查消息是否包含通知有效负载。
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // 触发您想要的任何操作。
}

有关更多信息,请查阅官方文档

英文:

Yes, you should use FCM as Google deprecated GCM.

Using FCM you can receive the notification and trigger an action in your device.

You'll need to register a service in your manifest:

<service
        android:name=".MyFirebaseMessagingService"
        android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

And then create the class MyFirebaseMessagingService that extends FirebaseMessagingService and override the onMessageReceived method:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Trigger any actions that you want.
}

For more information, please check the official documentation.

huangapple
  • 本文由 发表于 2020年9月14日 06:37:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63876212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定