如何排查:Android 客户端未收到 Azure 通知中心消息,但收到从 Firebase 控制台发送的测试消息

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

我正在尝试设置 Xamarin.Forms Android 客户端应用程序以接收来自 Azure C# 后端的推送通知。当我使用

GCMListenerService.OnMessageReceived()
从后端发送消息时,我的
SendGcmNativeNotificationAsync(...)
方法不会被触发,尽管结果返回成功/“入队”。我已成功接收来自同一服务器事件的 Windows 通知。

当我从 Firebase 控制台发送测试消息时会触发我的

GCMListenerService.OnMessageReceived()
方法,但当我从 Azure 控制台 -> 通知中心区域发送测试消息时不会触发(门户显示已成功发送)。

我可以确认我的客户从 Firebase 收到注册令牌并且可以使用 Google Play 服务。我正在物理设备上进行测试,而不是模拟器。

对于解决此问题的后续步骤有什么建议吗?不确定下一步要尝试什么。

我在

GCMListenerService.OnMessageReceived()
中使用 Log.Debug 语句来确定是否已收到消息。

感谢您的帮助!

azure xamarin.forms android-notifications azure-notificationhub firebase-notifications
2个回答
2
投票

我成功了。我没有意识到Android应用程序需要向通知中心注册;回想起来这很愚蠢。

通常这是由所有演练使用的 Xamarin 组件

GoogleCloudMessagingClient
来处理的,但我不想使用该组件,因为我正在学习并想查看样板代码。

对于采用这种方法的其他人,以下是我需要添加的内容(这不是要使用的复制粘贴代码,只是演示了我丢失的链接):

var cs = ConnectionString.CreateUsingSharedAccessKeyWithListenAccess(
      new Java.Net.URI("sb://YOUR-SERVICE-BUS.servicebus.windows.net/"),
      "YOUR-KEY"); // you can get these two values from Azure Console -> Your NotificationHub -> Settings -> Access Policies

    var hubName = "your-hub-name"; //the name of your Notification Hub resource on Azure

    hub = new NotificationHub(hubName, cs, context); //create the hub (this class is in WindowsAzure.Messaging namespace, which is accessible after you add the Azure Messaging component to your android project)

    hub.Register(registrationId, "YOUR_TAG"); //tag can be anything (alphanumeric, -, _, no spaces)
    //hub.Register registers this device and its registrationId (from cloud messaging) to the Notifications Hub.

这是您根据所有官方演练需要执行的所有其他操作的补充。我将在这里列出它们,因为有人可能会发现它有帮助。

步骤列表,Google 获取详细信息

Firebase:

  1. 创建新项目+设置推送通知

蔚蓝:

  1. 创建通知中心
  2. 从 Firebase 项目设置中向通知中心提供服务器密钥

Android客户端项目:

  1. 通过 NuGet 添加 Xamarin.GooglePlayServices.GCM
  2. 添加 Azure Messaging 组件(右键单击项目资源管理器中的“组件”,单击“获取更多组件”)
  3. 将以下权限添加到
    AndroidManifest.xml
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<permission android:name="[YOUR PACKAGE NAME HERE].permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="[YOUR PACKAGE NAME HERE].permission.C2D_MESSAGE" />
  1. MainActivity
    中,检查 Google Play 服务是否可用。
  2. 如果是这样,请创建一个
    RegistrationIntentService
    服务以向 Firebase Cloud Messaging 注册客户端设备
  3. MainActivity
    onCreate()
    方法中启动此服务
  4. 实现
    InstanceIDListenerService
    服务(如果应用程序注册ID发生变化,Google可以轻松了解详细信息)
  5. 实现
    GcmListenerService
    服务来接收云消息并做出反应(Google 即可轻松了解详细信息)
  6. AndroidManifest.xml
    中声明您的三个服务,例如
    GcmListenerService
    如下(轻松Google以获取更多详细信息):
<application android:label="YOUR_APP_NAME">
    <receiver android:name="com.google.android.gms.gcm.GcmReceiver" 
              android:exported="true" 
              android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="[YOUR APP PACKAGE NAME HERE]" />
        </intent-filter>
    </receiver>

  </application>

如果使用 Xamarin,您可以简单地使用

Service
属性,而不是编辑
AndroidManifest.xml
,这将为您将所需的声明添加到清单中,例如

[Service(Exported = false)]
class RegistrationIntentService : IntentService
{
  1. 创建
    NotificationHub
    实例并注册 Firebase 为设备返回的唯一令牌/registrationID(按照本文开头的代码,例如我丢失的链接)。

0
投票

xdfsddsdsefsfsdvsv xsfsff sgrferfsdfwefwefdfwefvvadwt356gnfhgafda

© www.soinside.com 2019 - 2024. All rights reserved.