0

I have written very simple code (just for demo) for push notification following google developers guide. I am sending notification using Postman but I can't receive it on my emulator. I am using blue stacks with google play services. Here is my code

    public class MainActivity extends AppCompatActivity {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

                     new Thread(new Runnable() {
                @Override
                public void run() {
                    String token = null;
                    InstanceID instanceID = InstanceID.getInstance(MainActivity.this);
                    try {
                        token = instanceID.getToken("590106578883",
                                GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
                    } catch (IOException e) {
                        Log.d("TAG", e.getMessage());
                    }
                    Log.d("TAG", token + "");
                    int i = 0;
                }
            }).start();
        } // onCreate



    } // MainActivity 

GcmListener.java

    public class MyGcmListenerService extends GcmListenerService {

            @Override
            public void onMessageReceived(String from, Bundle data) {
                sendNotification(data.toString());
            }

            private void sendNotification(String message) {
                Intent intent = new Intent(this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                        PendingIntent.FLAG_ONE_SHOT);

                Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                        .setContentTitle("GCM Message")
                        .setContentText(message)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
            }
        } // MyGcmListenerService

Manifest.java

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.topnotchdev.pushnotifications.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />


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

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="gcm.play.android.samples.com.gcmquickstart" />
        </intent-filter>
    </receiver>

    <service
        android:name=".MyGcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
</application>

Here is the response from google server

{
  "multicast_id": 7574264493447786438,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results": [
    {
      "message_id": "0:1459889454670774%d32a6865f9fd7ecd"
    }
  ]
}
mallaudin
  • 4,744
  • 3
  • 36
  • 68
  • Ok it's working fine on Google Nexus 5 but still I can't get notifications on Blue stacks emulator. – mallaudin Apr 06 '16 at 05:00
  • 1
    Possible duplicate of [Android emulator not receiving push notifications](http://stackoverflow.com/questions/20521600/android-emulator-not-receiving-push-notifications) – Tigger Apr 06 '16 at 05:01
  • referred question does not answer my question about Blue Stacks – mallaudin Apr 06 '16 at 10:08
  • I suggest you contact BlueStacks Support then as the response from Google is correct, which therefore points to either user error (in how you launch the emulator maybe?) or emulator software error. – Tigger Apr 06 '16 at 10:54

0 Answers0