【发布时间】:2015-05-28 12:06:24
【问题描述】:
我正在尝试在 android 上设置 Parse 推送通知一段时间。 我遵循了不同的教程,并尝试从他们的网络平台发送通知,但似乎没有任何效果。这是我到目前为止所尝试的。
这是我的 Application 类的 onCreate 方法
@Override
public void onCreate() {
super.onCreate();
Log.i("PushNotifications","PARSE initialize");
Parse.initialize(this, "******************************", "*****************************");
ParseInstallation.getCurrentInstallation().saveInBackground();
ParsePush.subscribeInBackground("", new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Log.i("PushNotifications","com.parse.push" + " successfully subscribed to the broadcast channel.");
} else {
Log.i("PushNotifications","com.parse.push" + " failed to subscribe for push");
}
}
});
}
这调用成功了,我得到了日志
已成功订阅广播频道。
这里还有我的清单文件的一些相关内容:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<permission android:name="com.my.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.my.app.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name=".App"
android:theme="@style/AppTheme">
<activity
android:name=".activities.MainActivity"
android:launchMode="standard"
android:screenOrientation="sensorPortrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.parse.push.gcm_sender_id" android:value="id:123456789" />
<meta-data android:name="com.parse.push.notification_icon" android:resource="@drawable/ic_launcher"/>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<!--com.parse.GcmBroadcastReceiver"-->
<receiver android:name="com.parse.GcmBroadcastReceiver"
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="com.my.app" />
</intent-filter>
</receiver>
</application>
当我尝试从 Parse 网站发送推送时,没有任何反应。
由于这不起作用,我尝试实现自己的 GcmBroadcastReceiver 并在清单中更改它。
<receiver android:name=".receivers.MyCustomReceiver"
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="com.my.app" />
</intent-filter>
</receiver>
和
public class MyCustomReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("PushNotifications","MyCustomReceiver");
}
}
没有成功。
然后我也尝试创建自己的 ParsePushBroadcastReceiver(通过从 ParsePushBroadcastReceiver 继承)。
<receiver android:name=".receivers.CustomParseReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
还有
public class CustomParseReceiver extends ParsePushBroadcastReceiver {
@Override
protected Notification getNotification(Context context, Intent intent) {
Log.i("PushNotifications","PARSE getNotification");
return null;
}
@Override
protected void onPushOpen(Context context, Intent intent) {
Log.i("PushNotifications","PARSE onPushOpen");
}
@Override
protected void onPushReceive(Context context, Intent intent) {
Log.i("PushNotifications","PARSE onPushReceive");
}
private JSONObject getDataFromIntent(Intent intent) {
Log.i("PushNotifications","PARSE getDataFromIntent");
}
}
它也不起作用。
问题是.. 当我创建自己的 GCM BroadcastReceiver 并从自己的服务器发送通知(使用一些小的 php 脚本)时,通知已成功接收。
我真的很想知道我的客户端解析通知系统的实现有什么问题。
关于问题可能来自哪里的任何提示?
【问题讨论】:
-
在广播接收器上尝试
android:exported="true" -
它会抛出 SecurityException:为了防止外部篡改您的应用程序的通知,所有注册处理以下操作的接收者必须将其导出属性设置为 false:com.parse.push.intent.RECEIVE, com .parse.push.intent.OPEN,com.parse.push.intent.DELETE
-
您的代码看起来不错。我认为你应该检查拼写错误。我使用 Parse 并卡住了 6 个小时,只是为了找出拼写错误“Player”“Players”。
-
请更改CustomParseReceiver目录并将其添加到MainActivity包目录中,添加后在manifest中进行更新。
-
Alex,下面这行可能是导致问题的原因。您不需要定义发件人 ID。删除它应该可以解决您的问题
标签: android parse-platform google-cloud-messaging