1

嗨,我正在尝试在接收广播消息时在 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());
        }
4

2 回答 2

2

我终于明白了:

  1. 对于没有 build() 方法:它是由 ActionBarSherlock 使用旧版本的 android-support-v4.jar 引起的。只需用 /SDK/extras/android/support/v4/android-support-v4.jar 中的更新后的 jar 替换该 jar

  2. 对于重复通知:它是由 Parse(parse.com) 服务引起的。当接收到任何在 JSON 数据中使用带有“标题”的解析通知库发送的推送时,服务会自动显示带有罐装 contentText 的通知,例如“您收到了通知”。这里我用来禁用自动通知的一个技巧是不要在解析推送的 JSON 数据中使用“标题”和“文本”,而是使用其他名称然后在自定义广播接收器中解析它。它现在工作正常。

谢谢!

于 2013-01-27T22:17:26.007 回答
0

http://developer.android.com/reference/android/app/Notification.Builder.html#build()

getNotification() 已弃用。为什么不使用 build() ?

于 2013-01-27T11:10:05.867 回答