推送通知到达ios(apns)但不是android(gcm)设备

时间:2015-06-14 01:09:30

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

所以我有一个简单的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

我确定我的应用名称是" MyApp"通过查看我的谷歌开发者控制台here并注意到"项目名称"期望的项目是" MyApp"。

我在同一网站上确定了Auth Key,导航到API & Auth -> Credentials -> API Key并从那里复制/粘贴API密钥。

我在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 Manifest以获得所有必要的权限

<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);
    }
}

我希望这些消息在发送推送通知后注销。但没有任何反应(服务器或应用程序没有抛出异常)。

任何人都能看到我在这里做错了吗?

3 个答案:

答案 0 :(得分: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);

要使上述代码正常工作,您需要在app /中设置google-services.json配置文件(由com.google.gms.google-services gradle插件解析,您还需要)目录,您需要gcm_defaultSenderId,它是您从Google Developers Console获得的项目ID(编号)。

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

获取注册ID的代码需要在here所述的IntentService中,您需要按照概述here在AndroidManifest.xml文件中定义服务。 为了使GCM能够与您的应用程序通信,您还需要在清单文件中定义com.google.android.gms.gcm.GcmReceiver,其中包含一个带有操作名称“com.google.android.c2dm.intent.RECEIVE”的意图过滤器和包名称的类别名称。请查看here示例。

答案 1 :(得分:0)

尝试由Google自己在这里运行示例。

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

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

希望有所帮助。

答案 2 :(得分:0)

如果其他人遇到此问题,问题是我没有在developers.google.com上将我的IP地址列入白名单。我现在已将它设置为所有IP都列入白名单,它就像一个魅力。