Я хочу синхронизировать свою активность с облаком Google Messaging. Когда сообщение GCM получает свой собственный приемник, получите сообщение и создайте уведомление, а затем передайте мое пользовательское сообщение получателю активности.Работа с синхронизацией с выпуском приемника BroadCast
В другой руке у моей деятельности есть собственный динамически зарегистрированный BroadcastReceiver, который получает мои служебные сообщения cusom.
Теперь это ситуация:
- , когда приложение открыто, без нажатия на уведомление, моя деятельность приемник принимает сообщение и шоу.
- , но когда активность закрыта после нажатия на уведомление, уведомление получает и приложение только открывается.
Я пытался МАЕ способы, как:
- зарегистрировать класс радиовещательный приемник На maifest. но я не могу синхронизировать его с моей деятельностью. потому что я обнаружил, что внешний приемник может sysnc с активностью только с putextra, а затем моя активность должна закрыться, а затем снова открыть, чтобы получить дополнительные возможности!
- попытайтесь передать свое пользовательское сообщение еще раз, создавая уведомление, но, похоже, не работает, потому что мне нужно широко использовать уведомление NotificationClick not onCreate.
- finnaly Я попытался зарегистрировать этот внутренний ресивер динамически на другой части, но он не может достижим.
так что если у вас возникла проблема, то какой лучший способ решения?
это моя в деятельности приемника:
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);
}