android手机未接收GCM psh通知

android手机未接收GCM psh通知,android,google-cloud-messaging,Android,Google Cloud Messaging,我很难将GCM推送通知集成到android应用程序中。我获得了设备id,服务器端似乎正常,因为它给出了成功消息。我认为问题在于android部分。请检查我的密码。 下面提供了我的清单文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.purpletab.sanghi">

我很难将GCM推送通知集成到android应用程序中。我获得了设备id,服务器端似乎正常,因为它给出了成功消息。我认为问题在于android部分。请检查我的密码。 下面提供了我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.purpletab.sanghi">

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>

    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.GET_TASKS" />

    <permission android:name="com.purpletab.sanghi.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.purpletab.sanghi.permission.C2D_MESSAGE" />


    <application
        android:name=".BaseApp"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:largeHeap="true"
        android:theme="@style/AppTheme"
        >
        <activity
            android:name=".activities.SplashActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.DashActivity"
            android:label=""
            android:theme="@style/AppTheme.NoActionBar"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden"/>
        <activity
            android:name=".activities.CoreActivity"
            android:label=""
            android:parentActivityName=".activities.DashActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.purpletab.sanghi.activities.DashActivity" />
        </activity>
        <activity android:name=".activities.SignInActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden"
            />
        <activity android:name=".activities.OtpActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden"></activity>
    </application>


    <receiver
        android:name="com.google.android.gcm.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.purpletab.sanghi" />
        </intent-filter>
    </receiver>
    <service android:name="com.purpletab.sanghi.GcmMessageHandler" />

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

</manifest>
GCMMessageHandler类如下所示

public class GcmMessageHandler extends IntentService implements Observer, AppConstants {

     String mes;
     private Handler handler;
    //private PushReceivingTemplate details;
    private BaseApp baseApp;

    public GcmMessageHandler() {
        super("GcmMessageHandler");

        System.out.println("HANDLER IS WORKING...");
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        handler = new Handler();

        baseApp = (BaseApp) getApplication();
        baseApp.getObserver().addObserver(this);

    }
    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();

        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        mes = extras.getString("message");
/*
        Gson gson = new Gson();
        Type collectionType = new TypeToken<PushReceivingTemplate>() {
        }.getType();
        details = gson.fromJson(mes, collectionType);

        final DatabaseHandler dbh = new DatabaseHandler(this);
        dbh.addNotidfications(details);

        //mes = getResources().getString(R.string.gcmPush);
        if(baseApp.getObserver().isAppIsInBackground(this))
        {
            NotificationUtils.displayNotification(this, details, baseApp);
        }
        //}*/

        System.out.println("____________________" + mes);
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    public void showToast(){
        handler.post(new Runnable() {
            public void run() {
                Toast.makeText(getApplicationContext(),mes , Toast.LENGTH_SHORT).show();
            }
         });
    }


    @Override
    public void update(Observable observable, Object data) {

    }
}

请解决此问题。

确保您的GcmMessageHandler与您在权限中定义的包相同,即com.purpletab.sanghi

您将收到服务器消息,因此没有机会保留任何配置问题

生成通知代码为:

     @Override
        protected void onHandleIntent(Intent intent) {
            Bundle extras = intent.getExtras();

            GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
            // The getMessageType() intent parameter must be the intent you received
            // in your BroadcastReceiver.
            String messageType = gcm.getMessageType(intent);

            mes = extras.getString("message");
    /*
            Gson gson = new Gson();
            Type collectionType = new TypeToken<PushReceivingTemplate>() {
            }.getType();
            details = gson.fromJson(mes, collectionType);

            final DatabaseHandler dbh = new DatabaseHandler(this);
            dbh.addNotidfications(details);

            //mes = getResources().getString(R.string.gcmPush);
            if(baseApp.getObserver().isAppIsInBackground(this))
            {
                NotificationUtils.displayNotification(this, details, baseApp);
            }
            //}*/

            System.out.println("____________________" + mes);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText(mes);
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ResultActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ResultActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());


            GcmBroadcastReceiver.completeWakefulIntent(intent);
        }
@覆盖
受保护的手部内容无效(意图){
Bundle extras=intent.getExtras();
GoogleCloudMessaging gcm=GoogleCloudMessaging.getInstance(this);
//getMessageType()意图参数必须是您收到的意图
//在你的收音机里。
字符串messageType=gcm.getMessageType(intent);
mes=额外的.getString(“消息”);
/*
Gson Gson=新的Gson();
Type collectionType=new-TypeToken(){
}.getType();
details=gson.fromJson(mes,collectionType);
final DatabaseHandler dbh=新的DatabaseHandler(this);
dbh.补充说明(详情);
//mes=getResources().getString(R.string.gcmPush);
if(baseApp.getObserver().isAppIsInBackground(this))
{
NotificationUtils.displayNotification(此、详细信息、baseApp);
}
//}*/
System.out.println(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
通知相容建筑商mBuilder=
新建NotificationCompat.Builder(此)
.setSmallIcon(R.drawable.notification_图标)
.setContentTitle(“我的通知”)
.setContentText(mes);
//为应用程序中的活动创建明确的意图
Intent resultIntent=新的Intent(这个,resultivity.class);
//stack builder对象将包含用于
//开始活动。
//这确保了从“活动”向后导航会导致
//您的应用程序将显示在主屏幕上。
TaskStackBuilder stackBuilder=TaskStackBuilder.create(此);
//为意图添加后堆栈(但不是意图本身)
addParentStack(ResultActivity.class);
//将启动活动的意图添加到堆栈顶部
stackBuilder.addNextIntent(resultIntent);
悬而未决的结果悬而未决的结果=
stackBuilder.getpendingent(
0,
PendingEvent.FLAG_更新_当前
);
mBuilder.setContentIntent(resultPendingContent);
通知经理通知经理=
(NotificationManager)getSystemService(上下文通知服务);
//mId允许您稍后更新通知。
mNotificationManager.notify(mId,mBuilder.build());
GcmBroadcastReceiver.completeWakefulIntent(intent);
}

希望这能对您有所帮助。

我怀疑这是否是问题所在,请查看下面的android开发者网站教程。谷歌更改了gcm方法配置。对于下层选民:对于下层选民:
{"multicast_id":6519739741263216481,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1457419626958900%8c03edcaf9fd7ecd"}]}
     @Override
        protected void onHandleIntent(Intent intent) {
            Bundle extras = intent.getExtras();

            GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
            // The getMessageType() intent parameter must be the intent you received
            // in your BroadcastReceiver.
            String messageType = gcm.getMessageType(intent);

            mes = extras.getString("message");
    /*
            Gson gson = new Gson();
            Type collectionType = new TypeToken<PushReceivingTemplate>() {
            }.getType();
            details = gson.fromJson(mes, collectionType);

            final DatabaseHandler dbh = new DatabaseHandler(this);
            dbh.addNotidfications(details);

            //mes = getResources().getString(R.string.gcmPush);
            if(baseApp.getObserver().isAppIsInBackground(this))
            {
                NotificationUtils.displayNotification(this, details, baseApp);
            }
            //}*/

            System.out.println("____________________" + mes);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText(mes);
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ResultActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ResultActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());


            GcmBroadcastReceiver.completeWakefulIntent(intent);
        }