【问题标题】:Capacitor/Ionic: Handling push notification in background or when app was killedCapacitor/Ionic:在后台或应用程序被终止时处理推送通知
【发布时间】:2020-10-13 10:27:25
【问题描述】:

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

我使用“PushNotifications”电容器插件 (https://capacitor.ionicframework.com/docs/apis/push-notifications/) 来收听来自 firebase 的推送通知(通知和数据类型),通知的收听非常顺利,即使在某些情况下一切都按预期运行应用程序被杀死或在后台。

问题如下:

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

  1. 如果应用在前台时收到通知,我可以使用运行自定义代码 addListener(eventName: "pushNotificationReceived", callback) 无论如何我都没有问题,因为该应用程序已打开。

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

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

似乎没有办法挂钩自定义代码,以便在后台收到推送通知或应用被终止时运行,你有没有遇到过这个问题?

谢谢!

【问题讨论】:

  • 有什么消息吗?我尝试在我的 Ionic Capacitor 应用程序的移动设备上禁用电池优化。然后,当应用程序被杀死时,我能够收到推送通知。我什至尝试再次启用优化和惊喜,推送通知也可以。似乎很奇怪。希望这不是最终的解决方案。
  • 你是如何在 Android 的前台获得通知的? github.com/ionic-team/capacitor/issues/2261 这表示前台通知不适用于 Android,只有在 iOS 中才能获得它们,我们必须使用本地通知...你能解释一下你是怎么做的吗?

标签: android ionic-framework ionic-native capacitor


【解决方案1】:

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

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

【讨论】:

  • 这对你还有效吗@Nour Krimesh?
  • 这就像一个魅力。主要问题是,在使用 FCM 时,您可能不会提供频道名称。
【解决方案2】:

我的解决方案是在 AndroidManifest.xml

中添加:FCM_PLUGIN_ACTIVITY 操作
        <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 并在需要时自己举杯。

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

【讨论】:

  • 您是否在 Android 的前台收到通知?有了那个实现?
  • 您收到它们但您必须手动显示它们,检查更新的答案。
【解决方案3】:

对我们来说,如果我们额外安装了 iOS 需要的 FCM 插件 (https://github.com/capacitor-community/fcm),后台通知(即使在 App 被杀死之后)也可以工作。

【讨论】:

    猜你喜欢
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 2018-06-30
    • 2020-09-01
    • 1970-01-01
    相关资源
    最近更新 更多