我试图在
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");
}
}
});
}
这被称为成功,因为我得到日志
successfully subscribed to the broadcast channel.
这里还有我的清单文件的一些相关内容:
<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,"PARSE getNotification");
return null;
}
@Override
protected void onPushOpen(Context context,"PARSE onPushOpen");
}
@Override
protected void onPushReceive(Context context,"PARSE onPushReceive");
}
private JSONObject getDataFromIntent(Intent intent) {
Log.i("PushNotifications","PARSE getDataFromIntent");
}
}
它也没有工作.
事情是..当我创建自己的GCM broadcastReceiver并且我从我自己的服务器发送通知(有一些小的PHP脚本),通知被成功接收.
我真的很想知道我实施客户端解析通知系统有什么问题.
任何暗示问题可能来自哪里?
解决方法
我的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..
}
}
和我的应用程序类:
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
}
希望它让你走上正确的道路!