【问题标题】:FirebaseMessagingService fails to start / be called in conjunction with CapacitorFirebaseMessagingService 无法启动/与 Capacitor 一起调用
【发布时间】:2022-10-06 04:31:13
【问题描述】:

所以我正在尝试使用 CapacitorJS 和 Ionic 构建一个应用程序。如果您没有听说过,Capacitor 是一个 JS 运行时,为 JavaScript 应用程序提供原生应用程序功能。 Capacitor 提供了一个 PushNotification 插件,但它不会像您通常那样在应用程序关闭时处理仅数据通知(Capacitor 必须调用 JavaScript,它不能这样做,没关系)。因此文档指出:

这个插件确实支持仅数据通知,但如果应用程序已被终止,则不会调用 pushNotificationReceived。要处理这种情况,您需要创建一个扩展 FirebaseMessagingService 的服务。

所以我试着做一个好公民,按照我被告知的去做,但问题是,我的服务没有被调用。接到电话的始终是电容器提供的服务,而我的服务则保持孤独和不受欢迎。我尝试将我自己的代码添加到 Capacitor 服务中,并且确实可以运行。但是,我不能发布本地通知,因为插件是不同的包。

所以我在这里有点麻烦。一方面,我需要 Capacitor 插件在应用程序打开时提供对 JavaScript 的调用,以便使用新消息响应地更新我的 UI。另一方面,当应用程序未打开并且 Capacitor 的服务似乎阻止了我的服务时,我需要一种方法来处理消息......不知何故。

我已经实施了谷歌搜索该问题时建议的所有更改,但无济于事。

安卓清单

<uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" />
<uses-permission android:name=\"android.permission.POST_NOTIFICATIONS\" />

<application
    android:allowBackup=\"true\"
    android:label=\"@string/app_name\"
    android:icon=\"@mipmap/ic_launcher\"
    android:roundIcon=\"@mipmap/ic_launcher_round\"
    android:supportsRtl=\"true\"
    android:theme=\"@style/AppTheme\">
    <meta-data android:name=\"com.google.firebase.messaging.default_notification_icon\"
        android:resource=\"@drawable/ic_notification_icon\" />
    <meta-data ...
    ...
    <service
        android:name=\"de.myDomainHere.ShufflechatFirebaseMessagingService\"
        android:exported=\"false\">
        <intent-filter>
            <action android:name=\"com.firebase.MESSAGING_EVENT\" />
        </intent-filter>
    </service>
    ...

我的服务

public class ShufflechatFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = \"shuffleNotifServ\";
    private int numMessages = 0;

    @Override
    public void onCreate(){
        Log.i(TAG, \"Firebase Messaging Service started\");
        // this /\\ never gets called
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage){
        super.onMessageReceived(remoteMessage);
        Log.i(TAG, \"From: \" + remoteMessage.getFrom());
        // this /\\ also never gets called
   
        //...do things to handle stuff
    }
}

.

为了方便任何愿意尝试帮助我的人,我将包括电容器插件的各个部分:

AndroidManifest

<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"
package=\"com.capacitorjs.plugins.pushnotifications\">

<application>
    <service android:name=\"com.capacitorjs.plugins.pushnotifications.MessagingService\" android:exported=\"false\">
        <intent-filter>
            <action android:name=\"com.google.firebase.MESSAGING_EVENT\" />
        </intent-filter>
    </service>
</application>
</manifest>

消息服务

public class MessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        PushNotificationsPlugin.sendRemoteMessage(remoteMessage);

        //If I add code here, it runs. I\'ve for example tried writing a file.
        //I shouldn\'t modify this however, because it will break on Capacitor updates.
    }

    @Override
    public void onNewToken(@NonNull String s) {
        super.onNewToken(s);
        PushNotificationsPlugin.onNewToken(s);
    }
}

摘自 PushNotificationsPlugin

public static void sendRemoteMessage(RemoteMessage remoteMessage) {
    PushNotificationsPlugin pushPlugin = 
        PushNotificationsPlugin.getPushNotificationsInstance();
    if (pushPlugin != null) {
        pushPlugin.fireNotification(remoteMessage);
        //This function /\\ is really just an interface to the WebView running JS.
    } else {
        lastMessage = remoteMessage;
    }
}

    标签: android firebase firebase-cloud-messaging android-service capacitor


    【解决方案1】:

    问题出在 Firebase 文档中。该服务应在其意图过滤器中列出com.google.firebase.MESSAGING_EVENT,而不是com.firebase.MESSAGING_EVENT.

    Firebase 团队已收到通知,文档已更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      • 2016-12-11
      • 1970-01-01
      • 2020-05-24
      • 2013-06-07
      相关资源
      最近更新 更多