PubNub Push messages not working

I am using GCM and Pubnub to send push notifications from one device to another. I am following the code given here -

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

I receive data sent from another device and can also display it. but the notification (in the notification bar) doesn't work.

After receiving the data, the Textview changes the display of the data, but nothing happens in the notification bar.

Below is the code for my broadcast receiver and GCM intent service (I'm 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 may be problems in the manifest file. 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 action (referenced in link) like register / unregister GCM etc. Please let me know where exactly the problem is. Thank you.

+3


source to share


1 answer


I found the problem.

I have mistakenly declared <receiver> application> in my manifest. Thus, the receiver did not register.



Thank.

+2


source







All Articles