如果您在 json 数据中使用“alert”或“title”,com.parse.PushService 将拦截并显示标准通知。
而是创建您自己的 BroadCastReceiver 并将标题发送为例如 json 中的“标题”。然后,您可以在 onReceive 处理程序中控制显示的时间和内容。
例如
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getAction();
String channel = intent.getExtras().getString("com.parse.Channel");
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
String title = "New alert!";
if (json.has("header"))
title = json.getString("header");
generateNotification(context, getImg(), title);
} catch (Exception e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
public static void generateNotification(Context context, int icon, String message) {
// Show the notification
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, SnapClientActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.vibrate = new long[] { 500, 500 };
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.flags =
Notification.FLAG_AUTO_CANCEL |
Notification.FLAG_SHOW_LIGHTS;
notificationManager.notify(0, notification);
}
}