Android BroadcastReceiver生成两个通知?
我试图使用BroadcastReceiver接收一个只包含文本的推送通知,它似乎同时生成了两个通知:一个是我正在推送的文本,另一个是我在NotificationCompat.Builder对象中包含的所有格式和样式。我知道这是BroadcastReceiver编码的固有特性,因为我已经注释掉了onReceive()方法的整个主体,它仍然会生成基本的通知。有没有办法抑制基本通知,使其只发送我正在手动生成的通知 这是我的密码:Android BroadcastReceiver生成两个通知?,android,push-notification,broadcastreceiver,Android,Push Notification,Broadcastreceiver,我试图使用BroadcastReceiver接收一个只包含文本的推送通知,它似乎同时生成了两个通知:一个是我正在推送的文本,另一个是我在NotificationCompat.Builder对象中包含的所有格式和样式。我知道这是BroadcastReceiver编码的固有特性,因为我已经注释掉了onReceive()方法的整个主体,它仍然会生成基本的通知。有没有办法抑制基本通知,使其只发送我正在手动生成的通知 这是我的密码: public class MyCustomReceiver extend
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()类的代码:
}请包括您的
generateNotification()
代码。抱歉,那应该是**receiveNotification()。这与您帮助回答的上一个问题的代码相同:)我要检查两件事:1。确保服务器不会向同一设备发送两次相同的通知(例如,如果数据库包含同一设备的多个注册ID,则可能会发生这种情况)。2.尝试将onReceive
的上下文参数传递给receiveNotification(),并使用它而不是this
(我不确定这是否会有任何区别,但我就是这么做的)。
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;