点击通知发送广播 [英] Send broadcast on notification click

本文介绍了点击通知发送广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本上是webview和GCM通知的应用程序。我想实现以下目标:如果用户在应用程序中并收到通知,那么当他点击通知时,我希望webview加载通知中提供的网址。



我在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接收到的);

}
};

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,通知);

我不明白为什么没有调用MainActivity类中的onReceive。不显示收到通知消息。
你能帮我吗?谢谢:)

解决方案

现在找不到原因,但有安全原因。最新的Android版本不会让你从种类远程过程中触发一个监听器,而不需要明确说明。

对于它的工作,intentyou广播必须是明确的。显式意味着您必须显式调用将处理意图的组件(接收者)。所以这个接收者必须在它自己的类和清单中声明为< receiver>



这个人在显式广播内容部分中的例子 http://codetheory.in/android-broadcast-receivers/
并且您将完成

I have an application which is basically a webview and GCM notifications. I want to achieve the following thing: If the user is in the app and receives a notification, when he clicks the notification I want the webview to load the url provided in the notification.

I'm trying to accomplish this by using broadcast receiver but it doesn't work.

I dynamically register the receiver in the 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);
    }

And in the GCM Listener I'm using 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);

I don't understand why onReceive in the MainActivity class is not called. The message "notification received" is not displayed. Can you help me? Thank you :)

解决方案

I cannot find right now the reason but there´s a security reason. Latest Android versions won´t allow you to trigger a listener from "kind-of" remote process without it being explicit.

The intentyou broadcast MUST be explicit for it to work. Explicit means that you have to explicitly call the component that will handle the intent (the receiver). So this receiver must be declared in its own class and in the manifest as a <receiver>.

Follow this guy´s example in the section Explicit Broadcast Intents http://codetheory.in/android-broadcast-receivers/ and thy will be done

这篇关于点击通知发送广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆