GCM:服务器发送了消息,android客户端没有收到

时间:2016-01-23 11:21:45

标签: android google-cloud-messaging

我实施了GCM,它第一次运行良好,但现在它不起作用!!我还在线搜索了这个主题,并实施了herehere的建议。

这是我的Manifest.xml:

<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="ir.masnasoft.ahkam.permission.C2D_MESSAGE"
                android:protectionLevel="signature" />
    <uses-permission android:name="ir.masnasoft.ahkam.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
<receiver
            android:name="ir.masna.ahkam.managers.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION" />
                <category android:name="ir.masnasoft.ahkam" />
            </intent-filter>
        </receiver>
        <service android:name="ir.masna.ahkam.managers.GcmMessageHandler"
            android:enabled="true"/>

        <meta-data android:name="ir.masnasoft.ahkam.version"
                   android:value="@integer/google_play_services_version" />

GcmBroadcastReceiver.java:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // Explicitly specify that GcmMessageHandler will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GcmMessageHandler.class.getName());

        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

我的GCMMessageHandler将收到的消息显示为通知和Toast:

public class GcmMessageHandler extends IntentService {

    String mes;
    private Handler handler;
    public GcmMessageHandler() {
        super("GcmMessageHandler");
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        handler = new Handler();
    }
    @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");
        // push notification
        NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher) // notification icon
                .setContentTitle("مساله گو") // title for notification
                .setContentText(mes) // message for notification
                .setAutoCancel(true); // clear notification after click
        //Intent intent = new Intent(this, Lun.class);
        //PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
        //mBuilder.setContentIntent(pi);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(alarmSound);

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());
        //
        showToast();
        Log.i("GCM", "Received : (" +messageType+")  "+extras.getString("message"));

        GcmBroadcastReceiver.completeWakefulIntent(intent);

    }

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

    }
}

我的服务器端正确发送了messege [id = 0:1453547161124866%7fba379966d6cf16]并正确注册我的设备。整个过程在过去两个小时内运行良好,但现在不起作用。

0 个答案:

没有答案