iFo*_*sts 5 android push-notification parse-platform
我正在使用Parse.com的推送通知服务.根据文件:
覆盖onPushReceive以触发"静默"推送的后台操作
我在这里找到了onPushOpen()的源代码,但现在我必须重写onPushReceive()来自定义声音和振动的行为.我不知道我应该在onPushReceive()中做什么,是否有任何示例代码可以帮助我找出onPushReceive()中的逻辑?谢谢.
Pat*_*orn 18
创建一个扩展ParsePushBroadcastReceiver的新类:
public class MyPushBroadcastReceiver extends ParsePushBroadcastReceiver {
public static final String PARSE_DATA_KEY = "com.parse.Data";
@Override
protected Notification getNotification(Context context, Intent intent) {
// deactivate standard notification
return null;
}
@Override
protected void onPushOpen(Context context, Intent intent) {
// Implement
}
@Override
protected void onPushReceive(Context context, Intent intent) {
JSONObject data = getDataFromIntent(intent);
// Do something with the data. To create a notification do:
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle("Title");
builder.setContentText("Text");
builder.setSmallIcon(R.drawable.ic_notification);
builder.setAutoCancel(true);
// OPTIONAL create soundUri and set sound:
builder.setSound(soundUri);
notificationManager.notify("MyTag", 0, builder.build());
}
private JSONObject getDataFromIntent(Intent intent) {
JSONObject data = null;
try {
data = new JSONObject(intent.getExtras().getString(PARSE_DATA_KEY));
} catch (JSONException e) {
// Json was not readable...
}
return data;
}
}
Run Code Online (Sandbox Code Playgroud)
在你的Manifest中添加:
<receiver
android:name=".MyPushBroadcastReceiver"
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>
Run Code Online (Sandbox Code Playgroud)
更多信息:http://www.androidhive.info/2015/06/android-push-notifications-using-parse-com/