我正在从片段启动服务并将一些数据传递给它并从服务我调用PubNub进行通知然后在显示我发送sendbroadcast的通知之后但是在发送广播时它没有在活动页面中获得接收。
活动
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
} else if (intent.getAction().equals("sample-event")) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d("Pubnub Message", "Got message: " + message);
}
}
};
@Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filter.addAction("sample-event");
this.registerReceiver(mMessageReceiver, filter);
}
片段
Intent pubBunintent = new Intent(mContext,Pubnub.class);
pubBunintent.putExtra("AuthKeyArray",AuthkeyArray);
pubBunintent.putExtra("channelKeyArray",ChannelArray);
mContext.startService(pubBunintent);
服务
public class Pubnub extends Service {
private ArrayList<String> Auth;
private ArrayList<String> Channel;
private String PubNubSubscribeKey = "key";
private String PubNubPublishKey = "key";
private Context mContext = this;
@Override
public void onCreate() {
super.onCreate();
StartPubnub();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Auth = intent.getStringArrayListExtra("AuthKeyArray");
Channel = intent.getStringArrayListExtra("channelKeyArray");
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void StartPubnub() {
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.setSubscribeKey(PubNubSubscribeKey);
pnConfiguration.setPublishKey(PubNubPublishKey);
pnConfiguration.setSecure(true);
pnConfiguration.setConnectTimeout(10000);
pnConfiguration.setAuthKey(Auth.get(0));
PubNub pubnub = new PubNub(pnConfiguration);
// Grant PAM Permissions for channel and auth_key, User Level Grant
pubnub.grant()
.channels(Channel)
.authKeys(Auth) // the keys we are provisioning
.write(true) // allow those keys to write (false by default)
.read(true) // allow keys to read the subscribe feed (false by default)
.ttl(0)// how long those keys will remain valid (0 for eternity)
.async(new PNCallback<PNAccessManagerGrantResult>() {
@Override
public void onResponse(PNAccessManagerGrantResult result, PNStatus status) {
// PNAccessManagerGrantResult is a parsed and abstracted response from server
Log.d("pubnub", "onResponse: grant " + status.toString());
}
});
pubnub.addListener(new SubscribeCallback() {
@Override
public void status(PubNub pubnub, PNStatus status) {
if (status.getOperation() != null) {
switch (status.getOperation()) {
case PNSubscribeOperation:
case PNUnsubscribeOperation:
switch (status.getCategory()) {
case PNConnectedCategory:
Log.d("pubnub", "onResponse: connected ");
case PNReconnectedCategory:
Log.d("pubnub", "onResponse: reconnected ");
break;
case PNDisconnectedCategory:
case PNUnexpectedDisconnectCategory:
pubnub.reconnect();
break;
case PNAccessDeniedCategory:
case PNBadRequestCategory:
Log.d("pubnub", "onResponse: bad ");
pubnub.reconnect();
break;
default:
Log.d("pubnub", "onResponse: bad ");
pubnub.reconnect();
}
case PNHeartbeatOperation:
if (status.isError()) {
} else {
}
break;
default: {
}
}
} else {
status.getCategory();
}
}
@Override
public void message(PubNub pubnub, PNMessageResult message) {
if (message.getChannel() != null) {
Log.d("Pubnub", "message: " + message.toString() + "Channel" + message.getChannel());
NotificationCompat.Builder builder;
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
JsonObject jsonObject = new JsonObject();
JsonElement pushMessage;
jsonObject = message.getMessage().getAsJsonObject();
pushMessage = jsonObject.get("message");
builder = new NotificationCompat.Builder(mContext);
builder.setContentTitle("Sample")
.setContentText(pushMessage.toString())
.setSmallIcon(R.drawable.ic_notification);
notificationManager.notify(0, builder.build());
Constant.PushnoteReceived = true;
Intent intent = new Intent("sample-event");
intent.putExtra("message", pushMessage.toString());
//Put your all data using put extra
sendBroadcast(intent);
} else {
Log.d("Pubnub", "message complete: " + message.toString() + "Subscription" + message.getSubscription());
Log.d("Pubnub", "message: " + message.getMessage());
Log.d("Pubnub", "Time token: " + message.getTimetoken());
}
}
@Override
public void presence(PubNub pubnub, PNPresenceEventResult presence) {
}
});
//subscribe
pubnub.subscribe().channels(Channel).execute();
// Adding Device to Channel: Enable push notifications on provided set of channels.
pubnub.addPushNotificationsOnChannels()
.pushType(PNPushType.GCM)
.channels(Channel)
.deviceId("com.sample.deviceTokenKey")
.async(new PNCallback<PNPushAddChannelResult>() {
@Override
public void onResponse(PNPushAddChannelResult result, PNStatus status) {
// handle response.
if (status.isError()) {
Log.d("pubnub", "onResponse: push note ERROR");
Log.d("pubnub", "onResponse: push " + status.toString());
} else {
Log.d("pubnub", "onResponse: push " + status.toString());
}
}
});
}
}