我想捕获从解析发送的通知,然后根据解析发送的 json 数据创建通知。我尝试使用另一个 SO 答案用我自己的类更改默认解析的 GCM 接收器,但仍然只显示默认通知。
我的 list :
<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.mypackage.Notification"
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" />
<!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
<category android:name="com.mypackage" />
</intent-filter>
</receiver>
我的自定义接收器类:
public class Notification extends BroadcastReceiver {
private static final String TAG = "MyCustomReceiver";
private static final int NOTIFICATION_ID = 1;
public static int numMessages = 0;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
try {
String action = intent.getAction();
String channel = intent.getExtras().getString("com.parse.Channel");
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.i("mari ñapas", "got action " + action + " on channel " + channel + " with:");
if (action.equalsIgnoreCase("my.application.technoat.NEW_NOTIF")) {
String title = "title";
if (json.has("header"))
title = json.getString("header");
generateNotification(context, title, json);
}
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
private void generateNotification(Context context, String title, JSONObject json) {
Intent intent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
numMessages = 0;
NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_drawer)
.setContentTitle(title)
.setContentText("New Post in the Blog")
.setNumber(++numMessages);
mBuilder.setContentIntent(contentIntent);
mNotifM.notify(NOTIFICATION_ID, mBuilder.build());
}
}
我仍然可以收到通知,但它不是由我的自定义类生成的,而是由默认解析的通知处理程序生成的。有人能指出问题出在哪里吗?
最佳答案
不幸的是,这里的答案有点陈旧。过去不可能“拦截”Parse 创建通知的方式,因此您必须创建自定义 JSON blob,它通常不会创建通知,而是广播 Intent 、监听该 Intent ,然后创建您自己的通知。这是 Android Judo,我们努力制作了 better API考虑到可定制性。
简而言之:按照更新的教程进行操作,该教程将要求您注册新的 com.parse.ParsePushBroadcastReceiver。要自定义行为,请注册您自己的 ParsePushBroadcastReceiver 子类,并根据您的喜好重载方法。如果您想在通知渲染方面发挥真正的创意,只需覆盖 getNotification
.
关于android - 自定义通过解析收到的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27215731/