我创建了一个Android应用程序。在这个应用程序中,我使用了推送通知概念。通知发送正确,并在接收端正确接收。但问题是相同的两个通知在接收端显示两次。请告诉我这是什么问题。
这是我的代码:
@Override
protected void onMessage(Context context, Intent intent)
{
Log.e(TAG, "Received message");
// String res=intent.getStringExtra("state");
//Log.e("res",res);
Log.e("full1 ",""+intent.getExtras().toString());
// Log.e("full2 ",""+intent.getData().toString());
//Log.e("full3 ",""+intent.toString());
String message="";
String id ="";
String name ="";
String state ="";
String desti ="";
String mobile_no ="";
String pickup_time="";
String unit="";
String street="";
String suburb="";
String city="";
String fare="";
String group="";
String date_of_creation="";
String sender_name="";
String sender_email="";
String sender_mobile="";
String sender_rating="";
try
{
// message = intent.getStringExtra("data");
//JSONObject jsonObject = new JSONObject(message);
// JSONArray array = new JSONArray(intent.getStringExtra("data"));
/*for (int i = 0; i < array.length(); i++)
{*/
//JSONObject jsonObject2 = array.getJSONObject(o);
id = intent.getStringExtra("id");
message = intent.getStringExtra("message");
name = intent.getStringExtra("name");
mobile_no = intent.getStringExtra("mobile_no");
unit = intent.getStringExtra("unit");
street = intent.getStringExtra("street_name");
suburb = intent.getStringExtra("suburb");
state = intent.getStringExtra("state");
date_of_creation = intent.getStringExtra("date_of_creation");
desti = intent.getStringExtra("destionation");
group = intent.getStringExtra("group");
pickup_time = intent.getStringExtra("pickup_time");
fare = intent.getStringExtra("fare");
city =intent.getStringExtra("city");
sender_name = intent.getStringExtra("sender_name");
sender_mobile = intent.getStringExtra("sender_mobile");
sender_email = intent.getStringExtra("sender_email");
sender_rating = intent.getStringExtra("sender_rating");
// }
}
catch(Exception e)
{
e.printStackTrace();
}
displayMessage(context, message);
generateNotification(context, name, id, state, desti, mobile_no,pickup_time,unit,street,suburb,city,fare,group,sender_name,sender_mobile,sender_email,sender_rating);
}
@Override
protected void onDeletedMessages(Context context, int total)
{
Log.i(TAG, "Received deleted messages notification");
String message = "Gcm deleted "+total;
displayMessage(context, message);
// notifies user
//generateNotification(context, message);
}
@Override
public void onError(Context context, String errorId)
{
Log.i(TAG, "Received error: " + errorId);
displayMessage(context, " Error "+ errorId);
}
@Override
protected boolean onRecoverableError(Context context, String errorId)
{
// log message
Log.i(TAG, "Received recoverable error: " + errorId);
displayMessage(context, "Gcm recovereable error" +errorId);
return super.onRecoverableError(context, errorId);
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
/*private static void generateNotification(Context context, String message)
{
int icon = R.drawable.windows;
long when = System.currentTimeMillis();
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(context).setSmallIcon(R.drawable.windows).setContentTitle(message).setContentText(message);
// Creates an explicit intent for an Activity in your app
SharedPreferences spf = context.getSharedPreferences("MYID", Context.MODE_PRIVATE);
String noti_ring = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString();
if(spf.getString("noti_ring", "").equals(""))
{
spf.edit().putString("noti_ring", noti_ring).commit();
}
else
{
noti_ring = spf.getString("noti_ring", "");
}
mBuilder.setSound(Uri.parse(noti_ring));
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(context, ViewRecievedJobs.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}*/
private static void generateNotification(Context context,String name, String id,String state,String desc,String mobileno, String picktime, String unit, String street, String suburb, String city, String fare, String group,String sender_name,String sender_mobile,String sender_email,String sender_rating)
{
int icon = R.drawable.windows;
long when = System.currentTimeMillis();
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(context).setSmallIcon(R.drawable.windows).setContentTitle(name).setContentText(name);
// Creates an explicit intent for an Activity in your app
SharedPreferences spf = context.getSharedPreferences("MYID", Context.MODE_PRIVATE);
String noti_ring = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString();
if(spf.getString("noti_ring", "").equals(""))
{
spf.edit().putString("noti_ring", noti_ring).commit();
}
else
{
noti_ring = spf.getString("noti_ring", "");
}
mBuilder.setSound(Uri.parse(noti_ring));
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(context, ViewRecievedJobs.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
System.out.println("Context "+context);
DatabaseAdmin admin = new DatabaseAdmin(context);
admin.insert_user(id, name, mobileno, picktime,unit,street,suburb, city, state,desc,fare,group,"1",sender_name,sender_mobile,sender_email,sender_rating);
// MyService.msg_id_list.add(id);
}
}
答案 0 :(得分:0)
在接收方,我指的是您接收推送通知的代码 GCMIntent服务的onMessage()方法会有逻辑 generationOfPushNotification
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, MainActivity.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.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
此代码用于生成推送通知,其中最后一行是
**notificationManager.notify(0, notification);**
表示发布要在状态栏中显示的通知。
其中 0 是id
和
通知是通知管理员的对象
每次执行通知生成代码时生成通知 通知的ID始终为
<强> 0 强>
这就是为什么它会两次或更多次显示相同的通知
所以你需要在其中传递静态变量而不是 0
所以行将是
notificationManager.notify(id, notification);
id++;
其中 id 是一个静态变量,因此当您下次收到通知时,其ID将不同
希望它能解决你为我工作的问题..