我有一个应用程序,基本上是一个Web视图和GCM通知。我想实现以下内容:如果用户在应用程序中并收到通知,则当他单击通知时,我希望Web视图加载通知中提供的URL。
我尝试使用广播接收器来实现这一点,但不起作用。
我在MainActivity中动态注册接收器:
我不明白为什么MainActivity类中的onReceive方法没有被调用。消息“notification received”没有显示。 你能帮忙吗?谢谢 :)
我尝试使用广播接收器来实现这一点,但不起作用。
我在MainActivity中动态注册接收器:
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方法没有被调用。消息“notification received”没有显示。 你能帮忙吗?谢谢 :)