简体   繁体   中英

PubNub Push messages not working

I am using GCM and Pubnub for sending push notifications from one device to other. I am following the code given here -

http://www.pubnub.com/blog/sending-receiving-android-push-notifications-with-gcm-google-cloud-messaging/

I am receiving the data sent from other device, and also able to display it. but Notification (in notification bar) is not working.

After I receive the data, Textview changes to show data, but nothing happens in Notification bar.

Following is code for my broadcast receiver and GCM intent service (I am guessing something might be wrong here)-

BroadcastReceiver

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver
{
  @Override
  public void onReceive(Context context, Intent intent)
  {
    ComponentName comp = new ComponentName(context.getPackageName(),
            GcmIntentService.class.getName());

    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
    Log.i("NIS", "Inside Brocast receiver");
  }
}

Service -

public class GcmIntentService extends IntentService {

private static final int NOTIFICATION_ID = 12345;

public GcmIntentService() {
    super("GcmIntentService");
}

@Override
protected void onHandleIntent(Intent intent)
{
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty() && GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType))
    {
        sendNotification("Received: " + extras.toString());
        Log.i("NIS", "Inside handle intent");
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg)
{
    NotificationManager mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, ChatsList.class), 0);

    Log.i("NIS", "send notification notification biulder");

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.kohls)
                    .setContentTitle("PubNub GCM Notification")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
 }
}

There could be problem in Manifest file too. Here is my Manifest file -

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.nishant.tkmabft.kconnect20.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.nishant.tkmabft.kconnect20.permission.C2D_MESSAGE" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".ChatsList"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ChatActivity"
        android:label="@string/title_activity_chat" >
    </activity>
</application>

<receiver
    android:name=".GcmBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="your.package.name" />
    </intent-filter>
</receiver>

<service android:name=".GcmIntentService" />
<meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

Other code inside Activity (given in the link provided) like GCM register/ unregister etc working fine. Please let me know where is exactly is problem. Thanks.

I found the problem.

I mistakenly declared < receiver > tag outside < application > tag in Manifest. So Receiver was not getting registered.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM