【发布时间】:2014-12-05 11:04:42
【问题描述】:
嘿,我正在制作一个用户可以相互发送 gcm 消息的应用程序 该应用程序运行良好,当我发送 gcm 消息时,接收者将其作为通知 当接收者点击它时,它会打开 homepage.class 但问题是 当他单击通知时,广播接收者没有收到它并且没有对 EditText 进行更改,另一方面,如果接收者在收到消息时正在使用 homepage.class,则对 EditText 进行更改(工作) 可能是什么问题呢 ??? 我的 GCMIntentService 中的一些方法
@Override
private static void generateNotification(Context context, String message) {
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, HomePage.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;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
@Override
protected void onMessage(Context context, Intent intent) {
String message = intent.getExtras().getString("message");
// notifies user
aController.displayMessageOnScreen(context, message);
generateNotification(context, message);
}
void displayMessageOnScreen(Context context, String message) {
Intent intent = new Intent("com.ms.gp.wefamily.DISPLAY_MESSAGE");
intent.putExtra("message", message);
// Send Broadcast to Broadcast receiver with message
context.sendBroadcast(intent);
}
这是我的 BroadcastReceiver 的代码
@Override
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString("message");
// Display message on the screen
Familyname.setText(newMessage);
}
};
如果有人知道答案,请告诉我(抱歉英语不好)
【问题讨论】:
-
这个问题我不清楚,为什么要通过点击通知来触发broadcastreceiver???
-
另外,你有一个名为 Familiyname 的类???还是应该是一个字段...
-
Familyname 是一个 EditText 对象,如果广播接收器未触发通知应该是什么?
-
广播接收器在收到来自 GCM 的消息时触发,onclick 部分类似于此线程stackoverflow.com/questions/15155804/…
标签: java android android-intent notifications google-cloud-messaging