我正在尝试在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脚本)时,通知已成功接收。
我真的很想知道我的客户端解析通知系统的实现有什么问题。
有关问题可能来自哪里的暗示?
答案 0 :(得分:3)
我的2美分(希望这有帮助):
我有一个类似的问题,我解决了(解释Here),所以这可能是问题,如果没有,看看我的文件,很快就看起来他们似乎有一些小的差异你的,他们工作所以值得一看:
清单部分:
<!-- for parse pushes -->
<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.MyStuff.stuff.utils.MyPushReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.OPEN" />
<action android:name="com.parse.push.intent.DELETE" />
</intent-filter>
</receiver>
<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.MyStuff.stuff" />
</intent-filter>
</receiver>
<meta-data
android:name="com.parse.push.notification_icon"
android:resource="@drawable/ic_launcher" />
我实现了自己的ParsePushBroadcastReceiver:
public class MyPushReceiver extends ParsePushBroadcastReceiver
{
@Override
protected void onPushOpen(Context context, Intent intent)
{
//bla bla...
}
@Override
protected void onPushReceive(Context context, Intent intent)
{
// bla bla...
}
@Override
protected Notification getNotification(Context context, Intent intent)
{
Notification notification = super.getNotification(context, intent);
// do some stuff with it..
}
}
和我的Application类:
public class MyApplication extends Application
{
private static final String CKey = "***********";
private static final String AId = "************";
@Override
public void onCreate()
{
super.onCreate();
Parse.initialize(this, AId, CKey);
//initialized some stuff..
ParsePush.subscribeInBackground("", new SaveCallback()
{
@Override
public void done(ParseException e)
{
if (null == e)
{
ParseInstallation install = ParseInstallation.getCurrentInstallation();
String token = install.getString("deviceToken");
//do some stuff with it...
}
}
});
ParseInstallation.getCurrentInstallation().saveInBackground();
//some more stuff
}
希望它能让你走上正确的道路!
答案 1 :(得分:2)
首先:您是否从项目设置中启用推送通知设置
我遇到了同样的问题,最后我删除了我的项目并在Parse中构建了新的项目,然后解决问题而不再做任何事情了!为什么?我不知道......
并且不要忘记总是获得最新的Parse SDK,现在它的Parse-1.9.2.jar