目前我正致力于推送。 我希望像whatsapp一样发出通知。
完美适用于单一通知。 当我从相同的ID获得另一个通知时,我希望在一个图标和标题(应用程序名称)下显示通知。
我在这里上传了我的代码。
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());
}
}
答案 0 :(得分:0)
将此信息包含在您的创建通知代码
中builder.setGroup("groupkey")
builder.setGroupSummary(true);