嗨,我正在尝试在接收广播消息时在 BroadcastReceiver 中发送通知。来自 Parse.com。但是当从 NotificationCompat.Builder 的一个实例调用 getNotification 方法时,通知会自动发送并显示一个预设的 contextText。因此,当调用 mNotificationManager.notify 时,会显示两个通知,一个带有指定的内容文本,另一个没有。我在 API 级别 16。
以下是我在 BroadcastReceiver 中的代码,请帮助我。非常感谢!
@Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getAction();
if (intent.getExtras() != null) {
JSONObject json = new JSONObject(intent.getExtras().getString(
"com.parse.Data"));
String text = json.getString("text");
String title = json.getString("title");
Long timestamp = json.getLong("timestamp");
String qid = json.getString("qid");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(text)
.setTicker("New Reply");
Intent resultIntent = new Intent(context,
QuestionViewActivity.class);
resultIntent.putExtra(QuestionViewActivity.EXTRA_QUESTION_ID,
qid);
TaskStackBuilder stackBuilder = TaskStackBuilder.from(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = mBuilder.getNotification();
mNotificationManager.notify(qid.hashCode(),
mBuilder.getNotification());
}