首页 文章

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

提问于
浏览
1

所以我有一个简单的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并注意到所需项目的"Project Name"是"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 回答

  • 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中,您需要在AndroidManifest.xml文件中定义服务,如here所示 .
    为了使GCM能够与您的应用程序通信,您还需要在清单文件中定义com.google.android.gms.gcm.GcmReceiver,其中包含一个带有操作名称"com.google.android.c2dm.intent.RECEIVE"的intent过滤器和一个带有您的包名称的类别名称 . 以here为例 .

  • 0

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

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

    我觉得你的清单文件中可能会遗漏一些东西 .

    希望有所帮助 .

  • 0

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

相关问题