1
我想將我的活動與Google雲消息同步。 當GCM消息接收它自己的接收者獲取消息並創建通知,然後將我的自定義消息廣播給活動接收者。與BroadCast Receiver問題同步活動
另一方面,我的Activity擁有自己動態註冊的BroadcastReceiver,它接收我的cusom消息。
現在這是一個情況:
- 當應用程序是開放的,沒有點擊通知,我的活動 接收器接收消息和節目。
- 但當活動關閉後點擊通知注意 接收和應用程序剛剛打開。
我試着像五月的方式:
- 註冊類廣播接收器在maifest。但我無法將其與我的活動同步。因爲我發現外接收器可以使用putextra進行活動,然後我的活動應該關閉,然後再次打開以獲得額外功能!
- 嘗試再次廣播我的自定義消息創建通知,但它似乎無法正常工作,因爲我需要在NotificationClick廣泛投上不onCreate通知。
- finnaly我試圖動態註冊這個內部reciver的另一部分但它無法訪問。
所以,如果你得到我的問題什麼是最好的解決辦法?
這是我在活動接收機:
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("LOG", "unreciver");
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
// Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
/**
* Take appropriate action on this message
* depending upon your app requirement
* For now i am just displaying it on the screen
* */
//Showing received message
//lblMessage.append(newMessage + "\n");
Log.i("LOG", "unreciver messsage:"+newMessage);
//Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
loadReciverDialog(newMessage);
// Releasing wake lock
WakeLocker.release();
}
};
這是從GCM接收消息並創建通知服務的一部分:
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
Log.i("LOG", "GCM service Message "+message);
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
private static void generateNotification(Context context, String message) {
Log.i("LOG", "genetaret notify");
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
,並且該部分顯示消息:
static void displayMessage(Context context, String message) {
Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
intent.putExtra(EXTRA_MESSAGE, message);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Log.i("LOG", "commonutils msg="+message);
context.sendBroadcast(intent);
}