【发布时间】:2015-08-11 09:00:26
【问题描述】:
我有一个基本上是 webview 和 GCM 通知的应用程序。我想实现以下目标:如果用户在应用程序中并收到通知,当他单击通知时,我希望 webview 加载通知中提供的 url。
我正在尝试通过使用广播接收器来完成此操作,但它不起作用。
我在MainActivity中动态注册receiver:
private void registerNotificationReceiver() {
final IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_LOAD_URL_FROM_NOTIFICATION);
Log.i(TAG, "registerNotificationReceiver()");
this.receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "notification received");
}
};
super.registerReceiver(this.receiver, filter);
}
在 GCM 监听器中,我正在使用 PendingIntent.getBroadast():
final Intent broadcastIntent = new Intent(MainActivity.ACTION_LOAD_URL_FROM_NOTIFICATION);
PendingIntent intent = PendingIntent.getBroadcast(getApplicationContext(), 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(intent);
notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notification);
我不明白为什么 MainActivity 类中的 onReceive 没有被调用。不显示消息“收到通知”。 你能帮助我吗?谢谢你:)
【问题讨论】:
标签: android push-notification broadcastreceiver google-cloud-messaging android-pendingintent