我正在尝试使用 BroadcastReceiver 接收仅包含文本的推送通知,它似乎同时生成了两个通知:一个只是我正在推送的文本,另一个包含我拥有的所有格式和样式包含在我的 NotificationCompat.Builder 对象中。我知道这是 BroadcastReceiver 编码方式固有的东西,因为我已经注释掉了我的 onReceive() 方法的整个主体,它仍然会生成基本通知。有没有办法抑制该基本通知,使其仅发送我手动构建的通知?
这是我的代码:
public class MyCustomReceiver extends BroadcastReceiver {
private static final String TAG = "MyCustomReceiver";
@Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getAction();
String channel = intent.getExtras().getString("");
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
Iterator itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
Log.d(TAG, "..." + key + " => " + json.getString(key));
if (key.equals("alert"))
{
//Save the message to the globalDataString
SplashActivity.globalDataString.push(json.getString(key).toString());
SplashActivity.notificationFlag = true;
//call the method that generates and sends the notification I want
receiveNotification();
return;
}
}
}
catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
}
任何建议,将不胜感激。谢谢!
*编辑:这是我的 receiveNotification() 类的代码:
public void receiveNotification() {
NotificationCompat.BigTextStyle bts = new NotificationCompat.BigTextStyle();
bts.bigText(SplashActivity.globalDataString);
bts.setSummaryText("Tap to open app, swipe to dismiss message");
NotificationCompat.Builder m = new NotificationCompat.Builder(this);
m.setContentTitle("New Push Notification")
.setContentText(SplashActivity.globalDataString)
.setSmallIcon(R.drawable.app_icon)
.setStyle(bts);
Intent openApp = new Intent(this, MenuActivity.class);
// This ensures that navigating backward from the Activity leads out of
// the application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MenuActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(openApp);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
m.setContentIntent(resultPendingIntent);
Notification noti = m.build();
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(pushMessageID, noti);
pushMessageID++;
//reset notification
flag1 = false;
}