我的應用程序接收來自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