我正在使用parse.com的推送通知服务。根据doc:
重写onpushreceive以触发“静默”的后台操作
推
我找到了onpushopen()here的源代码,但现在必须重写onpushreceive()来自定义声音和振动的行为。我不知道应该在onpushReceive()中做什么,有没有示例代码可以帮助我找出onpushReceive()中的逻辑?谢谢。
最佳答案
创建扩展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;
}
}
将此添加到清单中:
<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>
更多信息:http://www.androidhive.info/2015/06/android-push-notifications-using-parse-com/