Android 自定义通过解析收到的通知
我希望捕获从parse发送的通知,然后使用由parse发送的json数据创建通知。我尝试使用另一个SO答案用我自己的类更改默认解析的GCM接收器,但仍然只显示默认通知 我的舱单:Android 自定义通过解析收到的通知,android,parse-platform,google-cloud-messaging,android-notifications,Android,Parse Platform,Google Cloud Messaging,Android Notifications,我希望捕获从parse发送的通知,然后使用由parse发送的json数据创建通知。我尝试使用另一个SO答案用我自己的类更改默认解析的GCM接收器,但仍然只显示默认通知 我的舱单: 我仍然可以接收通知,但它不是由自定义类生成的,而是由默认解析的通知处理程序生成的。有人能指出问题出在哪里吗?如果您使用Parse发送通知。你为什么不使用 执行解析推送的示例代码: JSONObject data = new JSONObject("{\"alert\": \"The Mets scored!\",
我仍然可以接收通知,但它不是由自定义类生成的,而是由默认解析的通知处理程序生成的。有人能指出问题出在哪里吗?如果您使用Parse发送通知。你为什么不使用 执行解析推送的示例代码:
JSONObject data = new JSONObject("{\"alert\": \"The Mets scored!\",
\"badge\": \"Increment\",
\"sound\": \"cheering.caf\"}");
ParsePush push = new ParsePush();
push.setChannel("Mets");
push.setData(data);
push.sendPushInBackground();
代码是从许多答案中派生出来的,所以我可以将它们全部链接起来。感谢所有原创作者 这是我的第一个答案。所以,如果无论如何我可以改进它,请给我一些建议 因此,我通过捕获通知、分阶段读取json标记并构建通知来实现这一点 广播接收器类别:
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());
}
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
final Dbhelper p = new Dbhelper(context);
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("Got push", "got action " + action + " on channel " + channel + " with:");
if (action.equalsIgnoreCase("My.app.package")) {
String title = "title";
String message = "";
if (json.has("header"))
title = json.getString("header");
if (json.has("msg"))
message = json.getString("msg");
numMessages++;
p.addCircular(title, message);
generateNotification(context, title, message, json);
}
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
private void generateNotification(Context context, String title, String message, 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);
final NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_drawer)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.addAction(0, "Press to open", contentIntent)
.setAutoCancel(true)
.setDefaults(new NotificationCompat().DEFAULT_ALL);
//.setNumber(++numMessages);
mBuilder.setContentIntent(contentIntent);
mNotifM.notify(NOTIFICATION_ID, mBuilder.build());
}
}
将清单中解析的GCM接收器更改为
<receiver
android:name="My.app.package.MyBroadcastReciverClass"
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" />
<action android:name="com.sbu.sathyabama" />
<!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
<category android:name="My.app.package" />
</intent-filter>
</receiver>
不幸的是,这里的答案有点陈旧。过去不可能“截获”解析创建通知的方式,因此您必须创建自定义JSON blob,它通常不创建通知,而是广播意图,侦听该意图,然后创建您自己的通知。这是安卓柔道,我们努力制作了一个具有可定制性的游戏
简而言之:按照更新的教程进行操作,它将要求您注册新的com.parse.ParsePushBroadcastReceiver。要自定义行为,请注册您自己的ParsePushBroadcastReceiver子类,并将方法重载到您的核心内容。如果您想在通知呈现方面获得真正的创造性,只需重写即可。谢谢您的帮助,但这不是我想要的,因为我收到了来自parse的推送,希望从中提取数据并自行构建通知。答案如下:您是否曾尝试通过传递其对象id向特定用户发送推送通知。。。。。。。。。。。。。。。。。。。。
<receiver
android:name="My.app.package.MyBroadcastReciverClass"
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" />
<action android:name="com.sbu.sathyabama" />
<!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
<category android:name="My.app.package" />
</intent-filter>
</receiver>
{
"header": "My notification Title",
"msg": "My Notification message",
"action": "my.app.package"
}