无法接收推送通知(android)

时间:2016-04-05 21:02:08

标签: android google-cloud-messaging

我已经为谷歌开发人员指南之后的推送通知编写了非常简单的代码(仅用于演示)。我正在使用Postman发送通知,但我无法在我的模拟器上收到它。我正在使用蓝色堆栈与谷歌播放服务。这是我的代码

    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>

以下是来自Google服务器的回复

{
  "multicast_id": 7574264493447786438,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results": [
    {
      "message_id": "0:1459889454670774%d32a6865f9fd7ecd"
    }
  ]
}

0 个答案:

没有答案