Broadcast receiver not working broadcastmanager

时间:2017-07-12 08:19:47

标签: android android-broadcastreceiver

I am trying to send notification from broadcast receiver. My broadcast receiver is not working.

 final Intent intents = new Intent("tokenReceiver");
        // You can also include some extra data.
        final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
        intents.putExtra("token", token);
        intents.setClass(this, notificationreceiver.class);
        broadcastManager.sendBroadcast(intents);

Notificationreceiver class

public class notificationreceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String token = intent.getStringExtra("token");
        Log.d("token", token);
        // String token=FirebaseInstanceId.getInstance().getToken();
        if(token != null)
        {
            //send token to your server or what you want to do
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            Log.d("token",token);
            Firebase reference1 = new Firebase("https://fuudful-1a15c.firebaseio.com/users/" +user.getUid());
            Map<String, String> map = new HashMap<String, String>();
            map.put("registrationtoken", token);
            reference1.push().setValue(map);
        }

    }

}

Manifest

<receiver android:name=".notificationreceiver" android:enabled="true">
            <intent-filter>
                <action  android:name="tokenReceiver" />
            </intent-filter>
</receiver>

Can someone point why my broadcast is not working?

2 个答案:

答案 0 :(得分:0)

发送本地广播:

LocalBroadcastManager.getInstance(context).registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

            }
        }, new IntentFilter("YOUR_ACTION"));

在活动中本地接收:

#import "ProductModuleName-Swift.h"

答案 1 :(得分:0)

本地BroadcastReceiver不是真正的BroadcastReceiver,实际上它是一个回调函数列表。

您必须使用BroadcastReceiver。像这样:

    final Intent intents = new Intent("tokenReceiver");
    // You can also include some extra data.
    intents.putExtra("token", token);
    intents.setClass(this, notificationreceiver.class);
    sendBroadcast(intents);