电容器/离子:在后台或应用程序被终止时处理推送通知

问题描述 投票:0回答:4

大家早上好, 我几个小时都找不到解决方案。

我使用“PushNotifications”Capacitor 插件 (https://capacitor.ionicframework.com/docs/apis/push-notifications/) 来监听从 firebase 收到的推送通知(通知和数据类型),通知的收听效果非常好,即使应用程序被杀死或在后台运行,一切也会按预期运行。

问题如下:

我想在收到通知时打开应用程序,如果它在后台或已被杀死。

  1. 如果应用程序位于前台时收到通知,我可以使用以下命令运行自定义代码

    addListener(eventName: "pushNotificationReceived", callback)
    无论如何,我没有任何问题,因为应用程序是打开的。

  2. 如果应用程序处于background时收到通知,我可以强制应用程序保持backgroundMode处于活动状态(https://ionicframework.com/docs/native/background-mode) 并在收到通知后将应用程序置于前台。 (虽然我不太喜欢它,因为它很耗电池)

  3. 如果应用程序被杀死,我还没有找到问题的解决方案。

似乎没有办法挂钩自定义代码,使其能够在后台收到推送通知或应用程序被杀死时运行,您是否遇到过这个问题?

谢谢!

android ionic-framework ionic-native capacitor
4个回答
4
投票

我的解决方案是添加: FCM_PLUGIN_ACTIVITY 操作到 AndroidManifest.xml

        <activity
        ...
        android:name="com.appname.MainActivity"
        ...>

        ...
        ...
        ...

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

此外,在服务器上,您应该在推送通知对象中包含该操作:

  const pushNotification = {
  data: {
    ...
  },
  notification: {
    body: body,
    title: title,
    click_action: 'FCM_PLUGIN_ACTIVITY',
  },
};

在 ionic 项目中,您可以像下面一样处理 pushNotificationActionPerformed 并导航到所需的路线:

  PushNotifications.addListener('pushNotificationActionPerformed',
  (notification: PushNotificationActionPerformed) => {
    console.log('Push action performed: ' + JSON.stringify(notification));
  }
);

要在应用程序打开时处理接收推送通知,您应该处理 pushNotificationReceived 并在需要时自行显示 toast。

// the app is open, show your own notification if needed
PushNotifications.addListener('pushNotificationReceived',
  (notification: PushNotification) => {
    console.log('push notification received: ' + JSON.stringify(notification));
  }
);

0
投票

对于我们来说,如果我们另外安装了 iOS 所需的 FCM 插件 (https://github.com/capacitor-community/fcm),后台通知(即使在 App 被杀死后)也会起作用。


0
投票

我也遇到了同样的问题。它是消息发送方式的技术基础。这是一个 PHP 函数,用于将通知从服务器发送到设备。 “数据”是发送到应用程序的消息,“通知”是发送到 Android 操作系统。

function sendGCM($message, $id) {


    $url = 'https://fcm.googleapis.com/fcm/send';

    $fields = array (
            'registration_ids' => array (
                    $id // The device token
            ),
            'data' => array (
                    "message" => $message
            ),
            // After I put "notification", the device started to receive notifications in the background:
            'notification' => array (
                "title" => "App Title Notification",
                "body" => $message
            )
    );
    $fields = json_encode ( $fields );

    $headers = array (
            'Authorization: key=' . "MY_API_Cloud_Messaging_KEY", // App Key from console.firebase.google.com
            'Content-Type: application/json'
    );

    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_POST, true );
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
    echo $result;
    curl_close ( $ch );

}

0
投票

我遇到了同样的问题,并使用 AndroidManifest.xml 中的以下几行修复了它

<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="yourapp_notifications_channel_name" />
<service android:name="com.getcapacitor.CapacitorFirebaseMessagingService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

将“yourapp_notifications_channel_name”更改为您想要的名称,但您必须将其添加到电容器 PushNotification 中,如下所示:

if (this.platform.is('android')) {
  const channel: Channel = {
     id: 'yourapp_notifications_channel',
     name: 'yourapp_notifications_channel',
     sound: 'yourapp_notification_sound_file_name',
     importance: 5,
     visibility: 1,
     vibration: true
   };
   await PushNotifications.createChannel(channel);
}
PushNotifications.addListener('registration', async (fcmToken: Token) => {
/// your notificaiton logic
}```
© www.soinside.com 2019 - 2024. All rights reserved.