【问题标题】:Windows Azure notification hub, my app doesn't receive notificationsWindows Azure 通知中心,我的应用没有收到通知
【发布时间】:2014-12-25 15:33:04
【问题描述】:

我的应用程序似乎没有收到我发送给它的通知,使用天蓝色站点内的调试模式,发送测试通知,我也尝试通过一些也不起作用的 .net 代码发送通知,似乎就像我的通知中心正在接收这些通知,但我的手机上的应用程序没有,互联网正在运行,USB 调试已打开,并且我没有收到“收到 Herpderp 通知”的日志。

我已在通知中心成功注册我的应用,并正在等待使用以下代码的响应:

  gcm = GoogleCloudMessaging.getInstance(this);
    String connectionString = "***THIS IS where my connection string is, and it doesn't contain errors";
    hub = new NotificationHub("denlillemandhub", connectionString, this);
    NotificationsManager.handleNotifications(this, SENDER_ID, MyHandler.class);
    registerWithGcm();

registerWithGcm() 方法:

 private void registerWithGcm() {
    new AsyncTask() {
        @Override
        protected Object doInBackground(Object... params) {
            try {
                registrationId = gcm.register(SENDER_ID);
                Log.i(TAG, "Registered with id: " + registrationId);

            } catch (Exception e) {
                return e;
            }
            return null;
        }
        protected void onPostExecute(Object result) {
            Log.d("Done", "attempt to register was done loading");
            /**lblRegistration.setText(mRegistrationId);
            lblStatus.setText(getResources().getString(R.string.status_registered)); */
        };
    }.execute(null, null, null);
}

MyHandler 类(我将 notificationHandler 作为参数):

 import com.microsoft.windowsazure.notifications.NotificationsHandler;
  public class MyHandler extends NotificationsHandler
  {
  public static final int NOTIFICATION_ID = 1;
  private NotificationManager mNotificationManager;
  NotificationCompat.Builder builder;
  Context ctx;

   @Override
  public void onReceive(Context context, Bundle bundle) {
    ctx = context;
    String nhMessage = bundle.getString("msg");
    Log.d("line 27", "onReceive");
    sendNotification(nhMessage);
}

private void sendNotification(String msg) {
    Log.d("line 32", "sendNotification");
    mNotificationManager = (NotificationManager)
            ctx.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
            new Intent(ctx, ToDoActivity.class), 0);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(ctx)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("Notification Hub Demo")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

}

最后(我的 androidManifest 只是显示权限):

    <uses-permission android:name="android.permission.INTERNET"/>
<permission android:name="com.example.denlillemand.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.denlillemand.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />


<receiver android:name="com.microsoft.windowsazure.notifications.NotificationsBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />

        <category android:name="com.example.denlillemand" />
    </intent-filter>
</receiver>

我的建议是假设我在服务器端的脚本是正确的,并且 我的 GCM / Azure API 密钥/设置都是正确的。显然,我的通知集线器按预期接收,但似乎 GCM 要么没有传递到注册的设备,要么设备确实收到了通知,但没有正确处理它。

我要感谢任何想通读所有这些内容并给出合格猜测的人,非常感谢,我真的一直在努力解决这个问题。

谢谢。

【问题讨论】:

  • 您找到解决方案了吗?我也有同样的问题
  • 对不起 m8,调试太难了。突然我的朋友就开始工作了,他基本上没有改变任何东西,所以这真的很令人沮丧,我记得当时他不能给我答案。我最好的建议是仔细阅读文档,确保你的手机打开了互联网以及类似的愚蠢的小东西。否则我会在这里发布一个问题,详细说明您的确切情况。

标签: java android broadcastreceiver azure-mobile-services azure-notificationhub


【解决方案1】:

在您的 registerWithGcm() 方法中

registrationId = gcm.register(SENDER_ID);
Log.i(TAG, "Registered with id: " + registrationId);

添加这个:

hub.register(registrationId);

【讨论】:

  • 感谢您的回答,我在写完这篇文章 1 天后添加了那行代码。它仍然没有工作。最奇怪的是这个确切的代码 + hub.register(registrationId);与我的另一个项目一起工作。但是我放弃了这个项目,要通过太多代码来查找错误,Azure 服务器,notificationhub,c# 代码,java 代码,xml 和 GCM。所以我很抱歉,但我无法添加为什么它突然适用于任何正在寻找答案的人的原因。我希望我的示例代码可以帮助一些人试图弄清楚它。
猜你喜欢
  • 2014-07-15
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多