[英]receive multiple notifications and remove notification in android
在我的项目中,我正在处理通知。 我已成功收到多个通知,其注册ID很好。 我的问题是,当我使用具有相同注册ID的删除通知时,它在通知栏上仅显示一个通知。 我想在通知栏上显示所有通知,如果我选择一个通知,则必须将其从通知栏中删除。 请告诉我任何解决方案mycode:
RegIdDTO.java:
public class RegIdDTO {
public static final int REG_ID= (int) Calendar.getInstance().getTimeInMillis();
}
intentservice.java:
public class GcmIntentService extends IntentService{
Context context;
//System.currentTimeMillis();
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public static final String TAG = "GCM Demo";
public GcmIntentService() {
super("GcmIntentService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
String msg = intent.getStringExtra("message");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification(RegIdDTO.REG_ID,"Send error: " + extras.toString());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification(RegIdDTO.REG_ID,"Deleted messages on server: " +
extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i=0; i<5; i++) {
Log.i(TAG, "Working... " + (i+1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
//sendNotification("Received: " + extras.toString());
sendNotification(RegIdDTO.REG_ID,msg);
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(long when,String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent myintent = new Intent(this, ReceiveActivity.class);
myintent.putExtra("message", msg);
myintent.setData(Uri.parse("content://"+when));
PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle("Event tracker")
.setContentText("Events received");
Notification notification=mBuilder.build();
AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
/* Even if the mode is set to "Sound & Vibration" in the phone,
* the status code that getRingerMode() returns is RINGER_MODE_NORMAL.
*/
switch (am.getRingerMode())
{
case AudioManager.RINGER_MODE_VIBRATE:
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
break;
case AudioManager.RINGER_MODE_NORMAL:
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
break;
default:
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
}
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify((int) when, mBuilder.build());
// serv.CancelNotification(getApplicationContext(), (int) when);
}
public void CancelNotification(Context ctx) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx
.getSystemService(ns);
nMgr.cancel(RegIdDTO.REG_ID);
}
}
receiveactivity.java:
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive);
Intent intent = getIntent();
name = (TextView) findViewById(R.id.name);
deal = (TextView) findViewById(R.id.deal);
valid = (TextView) findViewById(R.id.valid);
address = (TextView)findViewById(R.id.address);
String message = intent.getExtras().getString("message");
try {
json = new JSONObject(message);
String stime = json.getString("name");
name.setText(stime);
String slecturename = json.getString("deal");
deal.setText(slecturename);
String sroom = json.getString("valid");
valid.setText(sroom);
String sfaculty = json.getString("address");
address.setText(sfaculty);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
serv=new GcmIntentService();
serv.CancelNotification(getApplicationContext());
}
}
尝试这个
private static void sendNotification(Context context, String message,
String keys, String msgId, String branchId) {
NotificationCompat.Builder nBuilder;
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
nBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("header")
.setLights(Color.BLUE, 500, 500).setContentText(message)
.setAutoCancel(true).setTicker("Notification from s")
.setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
.setSound(alarmSound);
// write your click event here
Intent resultIntent = new Intent(context, MainLoginSignUpActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
notify_no, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Show the max number of notifications here
if (notify_no < 9) {
notify_no = notify_no + 1;
} else {
notify_no = 0;
}
nBuilder.setContentIntent(resultPendingIntent);
NotificationManager nNotifyMgr = (NotificationManager) context
.getSystemService(context.NOTIFICATION_SERVICE);
nNotifyMgr.notify(notify_no + 2, nBuilder.build());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.