当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Generate one by one notifications under one title like Whatsapp

问题描述:

Currently I am working on pushnotifications.

I wish to make notification like whatsapp.

It works for single notification perfectly.

When I get another notification from same ID then I wish to display both notification under one Icon and title (App Name).

I upload my code here.

public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";

WebService w = new WebService();

JSONArray msendsuccess;

SharedPreferences preferences;

SharedPreferences.Editor editor;

String user_id/* , temp_id */;

int temp_id;

private final int NOTIFICATION_ID = 237;

private static int value = 0;

Notification.InboxStyle inboxStyle = new Notification.InboxStyle();

public ArrayList<String> msg;

public GCMIntentService() {

super(SENDER_ID);

}

@Override

protected void onRegistered(Context context, String registrationId) {

Log.e(TAG, "Device registered: regId = " + registrationId);

displayMessage(context, "Your device registered with GCM");

preferences = PreferenceManager.getDefaultSharedPreferences(context);

preferences.edit();

editor = preferences.edit();

user_id = preferences.getString("id", "");

msendsuccess = w.sendNotification(registrationId, user_id);

try {

SharedPreferences.Editor pref = preferences.edit();

pref.putString("registrationId", registrationId);

pref.commit();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

protected void onUnregistered(Context context, String registrationId) {

Log.i(TAG, "Device unregistered");

displayMessage(context, getString(R.string.gcm_unregistered));

}

@Override

protected void onMessage(Context context, Intent intent) {

Log.i(TAG, "Received message");

String message = intent.getExtras().getString("price");

// For notification ID

String id = intent.getExtras().getString("id");

if (id.equals("")) {

temp_id = 0;

} else {

temp_id = Integer.parseInt(id);

}

// temp_id = Integer.parseInt(id);

displayMessage(context, message);

// notifies user

generateNotification(context, message, temp_id);

}

@Override

protected void onDeletedMessages(Context context, int total) {

Log.i(TAG, "Received deleted messages notification");

String message = getString(R.string.gcm_deleted, total);

System.out.println("Message is" + message);

displayMessage(context, message);

// notifies user

generateNotification(context, message, temp_id);

}

@Override

public void onError(Context context, String errorId) {

Log.i(TAG, "Received error: " + errorId);

displayMessage(context, getString(R.string.gcm_error, errorId));

}

@Override

protected boolean onRecoverableError(Context context, String errorId) {

Log.e(TAG, "Received recoverable error: " + errorId);

displayMessage(context,

getString(R.string.gcm_recoverable_error, errorId));

return super.onRecoverableError(context, errorId);

}

private void generateNotification(Context context, String message, int id) {

int icon = R.drawable.notification_icon;

String title = context.getString(R.string.app_name);

NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification.Builder builder = new Notification.Builder(this);

builder.setContentTitle(title);

builder.setSmallIcon(icon);

builder.setAutoCancel(true);

builder.setGroup(title);

builder.setContentIntent(

PendingIntent.getActivity(context, id, new Intent(

context, JobCardDetailsNotification.class)

.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

.putExtra("id", id), 0));

builder.setGroupSummary(true);

inboxStyle.setBigContentTitle(title);

inboxStyle.setSummaryText(value+"");

for (int i = 0; i < value; i++) {

builder.setContentText(message);

inboxStyle.addLine(message);

}

value++;

builder.setStyle(inboxStyle);

nManager.notify(NOTIFICATION_ID, builder.build());

}

}

网友答案:

Include this into your create notification code

builder.setGroup("groupkey")
builder.setGroupSummary(true);
分享给朋友:
您可能感兴趣的文章:
随机阅读: