我的应用程序从GCM收到消息。
我已正确配置服务和广播接收器。然后,当消息即将发生时,我会显示通知。我有不同类型的消息的不同通知。运行正常。
好吧,现在我需要在收到新邮件时更新通知,例如Whatsapp。
例如,当Whatsapp从联系人收到一条消息时,显示短信," Hello world!"但是当从同一个联系人那里收到另一个时,更改通知信息,显示"两个新消息"。如果收到来自其他联系人的消息,通知会在2个聊天中显示" 3条消息"等等。
我需要对两种类型的消息做同样的事情,但不是全部。然后,我需要知道哪些通知显示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());
}
答案 0 :(得分:0)
要在应用收到PushNotification时更新视图,您需要LocalBroadCastManager
http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html
在Service&#34; GCMIntentService&#34;中,在onMessage方法中,你需要添加nextCode:
Intent intent = new Intent("Reload");
// You can also include some extra data.
intent.putExtra("message", "Reload");
LocalBroadcastManager.getInstance(getApplicationContext())
.sendBroadcast(intent);
这样就可以注册一个新的广播信息。
现在在更新课程中,您需要添加下一个: 在onCreate方法中:
LocalBroadcastManager.getInstance(getApplicationContext())
.registerReceiver(mMessageReceiver,
new IntentFilter("Reload"));
在您需要添加下一个方法之前:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
if (message.equalsIgnoreCase("Reload")) {
//Actions...for reload a listView, TextView, etc...
}
}
};
重要的是添加onDestroy方法:
@Override
public void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(getApplicationContext())
.unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
.... PD:抱歉我的英文......