Android - Azure 推送通知 Google FCM

Android - Azure Push Notifications Google FCM

我已经按照 Sending push notifications to Android with Azure Notification Hubs tutorial 使用 Azure 通知中心将推送通知实施到我的 Android 应用程序中。

之前,我使用另一个教程通过 Azure 通知中心使用 Google 云消息服务来执行此操作,但是当我将我的应用程序从 [=51] 发送到我的设备时,我只能收到推送通知=] 工作室。当我构建一个签名的 APK 并将其安装到我的设备上时,没有推送通知通过。

在昨天尝试了一整天让它工作之后(禁用 ProGuard,尝试不同的 API 键等)我决定今天早上重新开始。就在那时,我意识到 Google 现在在用户从 Cloud Console 单击 GCM 时会将用户定向到 Firebase Cloud Messaging。所以...我已经使用上述教程和 Google FCM 在我的应用程序中实现了推送通知。

效果很好....但是,当我创建一个签名的 APK 并将其安装在我的设备上而不是从 Android Studio 将应用程序发送到我的设备时,我没有收到任何推送通知。 Azure 显示推送已成功发送,但没有通过。

在我的推送处理程序中,我有一个控制台日志,如下面的 onReceive 方法所示。当我 运行 来自 Android Studio 的应用程序时,这被称为正常,并且推送按预期进行。但是当我从中创建签名的 apk 和 运行 时,不会调用 onReceive 方法并且没有推送。

@Override
    public void onReceive(Context context, Bundle bundle) {

        Log.d("TAG","TRIGGERED");

        ctx = context;
        String nhMessage = bundle.getString("message");
        String nhTitle = bundle.containsKey("title") ? bundle.getString("title") : "Title";
        String nhBadge = bundle.containsKey("badge") ? bundle.getString("badge") : null ;
        sendNotification(nhMessage,nhTitle,nhBadge);
        if (Main.isVisible) { Main.mainActivity.ToastNotify(nhMessage); }
    }

有人请帮助我。我错过了什么????? 是因为我是直接安装APK吗?是否必须从 Google Play 下载?除此之外,我不知道它会是什么。

更新

根据 Nikita G 的建议,我遵循了 instructions to send a test push from the command line using cURL
我得到的响应如下(看起来像一条成功消息)但是我的设备上没有推送。

{"multicast_id":6722521883082447284,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1473693614087201%06fb35f0f9fd7ecd"}]}

我的 cURL 请求如下所示,就像教程中显示的那样。

curl --header "Authorization: key=XXXXXXXXXXXX" --header "Content-Type: application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"fs...Tw:APA...SzXha\"]}"

更新

我刚刚注意到不仅仅是在我使用签名 APK 时。在 Android Studio 中,如果我使用 Build APK 选项生成 APK

然后使用位于 myappfolder/app/build/output/apk/ 的 app-debug.apk 文件安装,推送通知仍然不起作用。它们只有在我使用 运行 选项从 Android Studio 发送到我的设备时才有效。

经过数天的尝试,我发现我的 MyHandler class,即负责处理通过的推送的 class,并未声明为 public。

将 class 更改为 public 后,推送现在适用于构建和签名的 apks。

希望有人能解释为什么只有当应用程序是从 apk 文件安装时才会出现问题,因为那部分让我很困惑。

如果首先尝试了解问题是在通知中心端还是在 PNS(在本例中为 FCM)端,那么在此类问题中要遵循的一个重要方法。

一种方法是先通过 ANH diagnosis guidelines. If the issue is still not resolved, it may be helpful to try to push a message directly through the PNS to see whether that works or not. (In case of FCM, you can send a test push from the command line。)