我的应用程序接收来自GCM的消息。如何在可见时更新通知添加新信息?
我已经正确配置了一个服务和一个broadcastreceiver。然后,当一条消息正在提交时,我会显示一条通知。我有不同类型的消息通知。这运行良好。
好吧,现在我需要在收到新消息时更新通知,比如Whatsapp。
例如,当Whatsapp从联系人收到一条消息时,显示文本消息“Hello world!”但是当从同一联系人收到另一个联系人时,更改通知信息,显示“两条新消息”。如果收到来自其他联系人的消息,通知会显示“3条2条聊天消息”以及类似的消息。
我需要做两种类型的消息,但不是全部。然后,我需要知道哪些通知正在显示actionBar,然后更新而不是其他人。我想创建一个所有通知显示的bucle,分析它们,并检查是否有人显示我的具体类型,以前创建一个新的。
如何从notificationManager或StatusActionBar中获取信息以更改它?如何检查Actionbar上显示的通知是否具有相同类型的通知?
谢谢。
GCMIntentService.java
此服务分析来自GCM消息(notificationType)一个 “额外” 的值。制作一个开关盒,并为通知中的节目制作数据。
有一个显示通知的方法。我已经在showNotification方法上提出了一些意见,以说明我需要什么。
public class GCMIntentService extends IntentService {
private String TAG = this.getClass().getSimpleName();
private SQLiteDatabase localDB;
private SharedPreferences localSettings;
public GCMIntentService() {
super(StaticValues.GOOGLE_PROJECT_NUMBER);
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
int gcmNotificationType;
String gcmMess;
long gcmUserIDFrom;
long gcmUserIDTo;
String gcmUserFromCode;
String gcmEventShortDescription;
boolean showNotification = false;
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// Hay notificaciones de varios tipos así que las catalogamos y hacemos cosas diferentes en función del tipo
// Hay que recuperar los settings para saber qué notificaciones se tratan y cuáles no.
if (extras.getString("notificationType") != null) {
gcmNotificationType = Integer.valueOf(extras.getString("notificationType"));
gcmMess = extras.getString("message");
gcmEventShortDescription = extras.getString("eventShortDescription");
gcmUserFromCode = getString(R.string.app_name);
Log.d(TAG, "NotificationType: " + gcmNotificationType);
localSettings = getSharedPreferences(StaticValues.PREFS, 0);
switch (gcmNotificationType) {
case StaticValues.NOTIFICATION_MESSAGE_SINGLE:
Log.d(TAG, "Message received from " + extras.getString("userFromCode"));
localDB = PassObjects.getLocalDB();
gcmUserFromCode = extras.getString("userFromCode");
gcmUserIDFrom = Long.valueOf(extras.getString("userIDFrom"));
gcmUserIDTo = Long.valueOf(extras.getString("userIDTo"));
String inDate;
inDate = FormatValidators.convertTimeStampToString();
GenericMessageMethods.addMessages(gcmUserIDFrom, gcmUserIDTo, gcmMess, inDate, getApplicationContext(), localDB, false);
// Sólo llamo a la cola de broadcast de la pantalla
// si el mensaje es para el perfil en uso
if (gcmUserIDTo == PassObjects.getLOG_INFO_LAST_USER_ID()) {
Intent chatIntent = new Intent("com.example.JourneyApp.journeyActivities.LocalMessagesActivity");
chatIntent.putExtra("userIDFrom",extras.getString("userIDFrom"));
chatIntent.putExtra("userIDTo", extras.getString("userIDTo"));
chatIntent.putExtra("message", extras.getString("message"));
chatIntent.putExtra("messageDate", inDate);
sendBroadcast(chatIntent);
}
if (localSettings.getBoolean("TMP_NOTIFY_MESSAGES_FLAG",true)) {
if (localSettings.getBoolean("NOTIFY_MESSAGES_FLAG",true)) {
showNotification = true;
} else {
Log.d(TAG, "Notifications desactivated: "
+ "MessagesFlag: " + localSettings.getBoolean("NOTIFY_MESSAGES_FLAG",true));
}
} else {
Log.d(TAG, "Notifications TEMPORALLY desactivated. " +
" Processing messages with LocalMessagesActivity running");
}
break;
case StaticValues.NOTIFICATION_MESSAGE_ON_EVENT_ALL_ON_LINE:
Log.d(TAG, "Message received on event " + gcmEventShortDescription + " from " + extras.getString("userFromCode"));
gcmUserFromCode = extras.getString("userFromCode") + " " + getString(R.string.GCM_ON_EVENT) + " " + gcmEventShortDescription;
Intent foroIntent = new Intent("com.example.JourneyApp.journeyActivities.ForoMessagesActivity");
foroIntent.putExtra("userIDFrom",extras.getString("userIDFrom"));
foroIntent.putExtra("eventID", extras.getString("eventID"));
foroIntent.putExtra("message", extras.getString("message"));
foroIntent.putExtra("userFromCode", extras.getString("userFromCode"));
sendBroadcast(foroIntent);
if (localSettings.getBoolean("TMP_NOTIFY_EVENT_MESSAGES_FLAG",true)) {
if (localSettings.getBoolean("NOTIFY_EVENT_MESSAGES_FLAG",true)) {
showNotification = true;
} else {
Log.d(TAG, "Notifications desactivated: "
+ "EventMessagesFlag: " + localSettings.getBoolean("NOTIFY_EVENT_MESSAGES_FLAG",true));
}
} else {
Log.d(TAG, "Notifications TEMPORALLY desactivated: " +
" Processing messages with ForoMessagesActivity running");
}
break;
case StaticValues.NOTIFICATION_NEW_EVENT:
Log.d(TAG, getString(R.string.GCM_new_event_created) + " " + gcmEventShortDescription);
if (localSettings.getInt("NOTIFY_NEW_EVENTS_TYPE_ID",StaticValues.NOTIFICATION_ALWAYS) != StaticValues.NOTIFICATION_NEVER) {
if (localSettings.getInt("NOTIFY_NEW_EVENTS_TYPE_ID",StaticValues.NOTIFICATION_ALWAYS) == StaticValues.NOTIFICATION_ONLY_MY_INTEREST) {
if (PassObjects.getLOG_INFO_LAST_USER_ID() > 0) {
ArrayList<Integer> arrayLikesProfileLogged = PassObjects.getLOG_INFO_USER_LIKES();
ArrayList<Integer> arrayLikesOnEvent = new ArrayList<Integer>();
// Los extras están en String. Lo paso a un String[], lo recorro, parseo a int
// y añado al array. Recorro el del evento y en cuanto encuentro uno coincidente,
// salgo y muestro notificación
String extrasEventLikesString = extras.getString("eventLikes");
Log.d(TAG, "EventLikes: (string) " + extrasEventLikesString);
String[] auxS = extrasEventLikesString.replace(" ", "").split(",");
for (int i = 0; i < auxS.length; i++) {
arrayLikesOnEvent.add(Integer.parseInt(auxS[i]));
}
Log.d(TAG, "EventLikes: (ArrayList<Integer>) " + arrayLikesOnEvent.toString());
for (int x:arrayLikesOnEvent) {
if (arrayLikesProfileLogged.contains(x)) {
gcmMess = getString(R.string.mess_new_event_created);
showNotification = true;
break;
}
}
} else {
Log.d(TAG, "Notification is: " + localSettings.getInt("NOTIFY_NEW_EVENTS_TYPE_ID",StaticValues.NOTIFICATION_ALWAYS) + " but user not logged");
}
} else {
gcmMess = getString(R.string.mess_new_event_created);
showNotification = true;
}
} else {
Log.d(TAG, "Notifications desactivated: "
+ "Notify new events type: " + localSettings.getInt("NOTIFY_NEW_EVENTS_TYPE_ID", StaticValues.NOTIFICATION_ALWAYS));
}
break;
case StaticValues.NOTIFICATION_NEW_USER:
Log.d(TAG, "New user created: " + extras.getString("userFromCode"));
if (localSettings.getInt("NOTIFY_NEW_USERS_TYPE_ID", StaticValues.NOTIFICATION_ALWAYS) != StaticValues.NOTIFICATION_NEVER) {
if (localSettings.getInt("NOTIFY_NEW_USERS_TYPE_ID", StaticValues.NOTIFICATION_ALWAYS) == StaticValues.NOTIFICATION_ONLY_MY_INTEREST) {
if (PassObjects.getLOG_INFO_LAST_USER_ID() > 0) {
ArrayList<Integer> arrayLikesProfileLogged = PassObjects.getLOG_INFO_USER_LIKES();
ArrayList<Integer> arrayLikesOnUser = new ArrayList<Integer>();
// Los extras están en String. Lo paso a un String[], lo recorro, parseo a int
// y añado al array. Recorro el del evento y en cuanto encuentro uno coincidente,
// salgo y muestro notificación
String extrasUserLikesString = extras.getString("userLikes");
Log.d(TAG, "UserLikes: (string) " + extrasUserLikesString);
String[] auxS = extrasUserLikesString.replace(" ", "").split(",");
for (int i = 0; i < auxS.length; i++) {
arrayLikesOnUser.add(Integer.parseInt(auxS[i]));
}
Log.d(TAG, "UserLikes: (ArrayList<Integer>): " + arrayLikesOnUser.toString());
for (int x:arrayLikesOnUser) {
if (arrayLikesProfileLogged.contains(x)) {
gcmMess = getString(R.string.mess_profile_created_part1);
showNotification = true;
break;
}
}
} else {
Log.d(TAG, "Notification is: " + localSettings.getInt("NOTIFY_NEW_USERS_TYPE_ID", StaticValues.NOTIFICATION_ALWAYS) + " but user not logged");
}
} else {
gcmMess = getString(R.string.mess_profile_created_part1);
showNotification = true;
}
} else {
Log.d(TAG, "Notifications desactivated: "
+ "Notify new uers type: " + localSettings.getInt("NOTIFY_NEW_USERS_TYPE_ID", StaticValues.NOTIFICATION_ALWAYS));
}
break;
case StaticValues.NOTIFICATION_CHANGE_EVENT:
Log.d(TAG, "Changes on event: " + gcmEventShortDescription);
gcmMess = getString(R.string.GCM_changes_on_event) + " " + gcmEventShortDescription;
showNotification = true;
break;
case StaticValues.NOTIFICATION_LINK_EVENT:
Log.d(TAG, "Linked user from event: " + gcmEventShortDescription);
if (localSettings.getBoolean("NOTIFY_EVENT_LINK_FLAG", true)) {
gcmMess = getString(R.string.mess_linked) + " " + getString(R.string.GCM_ON_EVENT) + " " + gcmEventShortDescription;
showNotification = true;
} else {
Log.d(TAG, "Notifications desactivated: "
+ "Notify link event: " + localSettings.getBoolean("NOTIFY_EVENT_LINK_FLAG", true));
}
break;
case StaticValues.NOTIFICATION_UNLINK_EVENT:
Log.d(TAG, "Unlinked user from event: " + gcmEventShortDescription);
if (localSettings.getBoolean("NOTIFY_EVENT_UNLINK_FLAG", true)) {
gcmMess = getString(R.string.mess_unlinked) + " " + getString(R.string.GCM_ON_EVENT) + " " + gcmEventShortDescription;
showNotification = true;
} else {
Log.d(TAG, "Notifications desactivated: "
+ "Notify unlink event: " + localSettings.getBoolean("NOTIFY_EVENT_UNLINK_FLAG", true));
}
break;
case StaticValues.NOTIFICATION_EVENT_CAPACITY_FULL:
Log.d(TAG, "Capacity full on event: " + gcmEventShortDescription);
if (localSettings.getBoolean("NOTIFY_EVENT_CAPACITY_FULL_FLAG", true)) {
gcmMess = getString(R.string.GCM_event_capacity_completed) + " " + gcmEventShortDescription;
showNotification = true;
} else {
Log.d(TAG, "Notifications desactivated: "
+ "Notify event capacity full: " + localSettings.getBoolean("NOTIFY_EVENT_CAPACITY_FULL_FLAG", true));
}
break;
case StaticValues.NOTIFICATION_EVENT_WILL_BEGIN_AT:
Log.d(TAG, "Begin notification on event: " + gcmEventShortDescription);
gcmMess = getString(R.string.GCM_event_will_begin_part_1) + " " + gcmEventShortDescription + " " + getString(R.string.GCM_event_will_begin_part_2);
showNotification = true;
break;
case StaticValues.NOTIFICATION_EVENT_CANCELLED:
Log.d(TAG, "Cancel event: " + gcmEventShortDescription);
gcmMess = getString(R.string.GCM_event_canceled) + " " + gcmEventShortDescription;
showNotification = true;
break;
default:
Log.d(TAG, "Notification not in case");
break;
} //END Switch
if (showNotification) {
showNotification(gcmNotificationType, gcmMess, gcmUserFromCode);
}
} else {
Log.d(TAG, "Cannot find <notificationType> label on extras");
Log.d(TAG, "Extras.size(): " + extras.size() + ", extrasToString " + extras.toString());
}
} else {
Log.d(TAG, "Other GCM message type received");
}
} else {
Log.d(TAG, "Extras are empty");
}
GCMBroadcastReceiver.completeWakefulIntent(intent);
}
private void showNotification(int nType, String mMessage, String mTitle) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// I would like to analyze ActionBar data or NotificationManager data
// Something like...
// for (x:CurrentVisibleNotifications) { // Which will be this object?
// int currentId = x.getId();
// int currentNumber = x.getNumber(); // This is a property of notification
// if (currentId == nType) {
// mMessage = currentNumber++ + " new messages";
// }
// }
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.journey_icon_orange)
.setContentTitle(mTitle)
.setContentText(mMessage)
.setTicker(getString(R.string.app_name))
.setAutoCancel(true)
;
// Intent notIntent = new Intent(this, MainActivity.class);
// PendingIntent contIntent = PendingIntent.getActivity(this, 0, notIntent, 0);
// mBuilder.setContentIntent(contIntent);
mNotificationManager.notify(nType, mBuilder.build());
}
这不能解决我的问题。 – Carlos 2014-10-09 09:17:59
一年后:D没有关于它的消息吗?我有同样的“问题”.. – deveLost 2015-09-24 11:20:49