【问题标题】:Push notification reaching ios (apns) but not android (gcm) device推送通知到达 ios (apns) 但未到达 android (gcm) 设备
【发布时间】:2015-08-29 18:19:47
【问题描述】:

所以我有一个简单的 Ruby 应用程序,它使用 Rpush 向我的 iphone 和我的 android 手机发送推送通知。现在它所做的只是将它发送到 iPhone。我不确定问题是否与我的脚本有关(即 registration_idapp_nameauth_key 的值不正确),或者问题是否在于我的 Android 应用程序配置方式。

该代码的相关部分在这里(为安全起见更改了值 - 但键的格式/长度保持不变,以确保它们对有经验的人“看起来正确”)

API SETUP/RATIONALE(发送通知)

# GCM
app = Rpush::Gcm::App.new
app.name = "MyApp"
app.auth_key = "POfaSyfghilK3l-ueSvRLmbawcRThCWkwmcYGeM"
app.connections = 1
app.save

n = Rpush::Gcm::Notification.new
n.app = Rpush::Gcm::App.find_by_name("MyApp")
n.registration_ids = ["derGV80JK-s:APA91bHgerskBnrhHndes947nMKxI116tC3-k-tGd-hT5NzVc8QAWEkvCrMwrvs78RUL8-vvhp2ultoevqzZnn8gsr9t6WDXDYpGfCliqaJzj0XByBgbi0bm-rYufjcxfCc_5lEL381F"]
n.data = { message: "testing!" }
n.save!

Rpush.push

通过查看我的谷歌开发者控制台here 并注意到所需项目的“项目名称”是“MyApp”,我确定我的应用程序的名称是“MyApp”。

我通过导航到 API & Auth -> Credentials -> API Key 并从那里复制/粘贴 API 密钥来确定同一站点上的 Auth Key。

我在我的 Android 应用程序的主要活动中使用此代码确定了我的设备的注册 ID:

public static String getDeviceID(Context context) {
    final TelephonyManager tm = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);

    final String tmDevice, tmSerial, tmPhone, androidId;
    tmDevice = "" + tm.getDeviceId();
    tmSerial = "";// + tm.getSimSerialNumber();
    androidId = ""
            + android.provider.Settings.Secure.getString(
            context.getContentResolver(),
            android.provider.Settings.Secure.ANDROID_ID);

    UUID deviceUuid = new UUID(androidId.hashCode(),
            ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
    String deviceId = deviceUuid.toString();

    return deviceId;
}

登录后,getDeviceID 会显示我在上述 ruby​​ 代码中指定的注册 ID。

APP SETUP/RATIONALE(接收通知)

首先,我将我的 Android 清单设置为拥有所有必要的权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.johncorser.myapp.permission.RECEIVE" />
<permission android:protectionLevel="signature"
    android:name="com.johncorser.myapp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.johncorser.myapp.permission.C2D_MESSAGE" />

然后,我设置了一个监听器服务来响应推送通知:

    <service
        android:name="com.johncorser.myapp.services.GcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

那个类很简单,看起来像这样:

public class GcmListenerService extends com.google.android.gms.gcm.GcmListenerService {
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Log.d("push", "From: " + from);
        Log.d("push", "Message: " + message);
    }
}

我希望这些消息在发送推送通知后注销。但是什么都没有发生(服务器或应用程序上没有抛出异常)。

有人看到我在这里做错了吗?

【问题讨论】:

    标签: android push-notification google-cloud-messaging


    【解决方案1】:

    万一其他人遇到这种情况,问题是我没有在developers.google.com 上将我的IP 地址列入白名单。我现在已经设置好了,所以所有的 IP 都被列入白名单,它就像一个魅力。

    【讨论】:

      【解决方案2】:

      您为什么使用从 Android 设备 ID 生成的 UUID 作为 Google Cloud Messaging 的注册 ID? 这不是您获得注册 ID 的方式。

      要获得注册 ID,您必须在设备上注册 GCM 并收到注册 ID/令牌,如Cloud Messaging for Android Quickstart 中所述:

      InstanceID instanceID = InstanceID.getInstance(this);
      String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
              GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
      

      为了使上述代码正常工作,您需要在您的应用程序/目录,您需要 gcm_defaultSenderId,它是您从 Google Developers Console 获得的项目 ID(编号)。

      您可以通过单击“Get a configuration file”按钮并按照其中提到的步骤轻松生成该文件并接收上述详细信息。

      获取注册 ID 的代码需要在 IntentService 中,如 here 所述,您需要在 AndroidManifest.xml 文件中定义服务,如 here 所述
      为了让 GCM 能够与您的应用程序通信,您还需要在清单文件中定义一个 com.google.android.gms.gcm.GcmReceiver,它具有一个带有动作名称“com.google.android.c2dm.intent.RECEIVE”和类别名称的意图过滤器用你的包名。以here 为例。

      【讨论】:

        【解决方案3】:

        尝试由 Google 自己在此处运行示例。

        https://developers.google.com/cloud-messaging/android/start https://github.com/googlesamples/google-services/tree/master/android/gcm

        我觉得您的清单文件中可能缺少某些内容。

        希望对您有所帮助。

        【讨论】:

          猜你喜欢
          • 2023-03-17
          • 2016-03-08
          • 1970-01-01
          • 1970-01-01
          • 2015-11-21
          • 1970-01-01
          • 1970-01-01
          • 2014-10-21
          • 1970-01-01
          相关资源
          最近更新 更多