WakefulBroadcastReceiver的onRecei

WakefulBroadcastReceiver的onRecei

本文介绍了WakefulBroadcastReceiver的onReceive不触发的Andr​​oid GCM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施 GCM 的通知在Android和一切工作正常。我通过使用谷歌云消息服务的PHP服务器发送通知到手机上。结果
一个星期左右后,我再次安装该应用程序在手机上,并发送测试通知到我的手机,但我什么也没得到。 PHP脚本没有用成功= 1 并没有cononical的ID在GCM响应的任何错误运行。 WakufulBroadcastReceiver 在Android应用程序不会被触发。

I implemented GCM notifications in Android and everything was working fine. I'm sending Notifications to the phone via a PHP server using the Google Cloud Messaging Service.
After a week or so I installed that app again on my phone and send a test notification onto my phone but I got nothing. The PHP script runs without any error with success=1 and no cononical id's in the GCM response. WakufulBroadcastReceiver isn't triggered in the android app.

下面是完整的code,我现在用:

Here is the complete code that I am using:

GcmBroadcastReceiver.java

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("", "In Receive Method of Broadcast Receiver");
        ComponentName cn = new ComponentName(context.getPackageName(), GcmMessageHandler.class.getName());
        startWakefulService(context, intent.setComponent(cn));
        setResultCode(Activity.RESULT_OK);
    }
}

GcmMessageHandler.java

public class GcmMessageHandler extends IntentService{

    Handler handler;
    public GcmMessageHandler() {
        super("GcmMessageHandler");
        // TODO Auto-generated constructor stub
    }
    public GcmMessageHandler(String name) {
        super("GcmMessageHandler");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        handler = new Handler();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);
        final String message = intent.getExtras().getString("name");
         Log.d("GCM", "Received : (" +messageType+")  "+intent.getExtras().getString("name"));
        handler.post(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
            }
        });
        GcmBroadcastReceiver.completeWakefulIntent(intent);

    }

}

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test_push_app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET" />
    <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" />
    <permission
        android:name="com.example.test_push_app.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.test_push_app.permission.C2D_MESSAGE" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <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="com.example.test_push_app" />
            </intent-filter>
        </receiver>
        <service android:name=".GcmMessageHandler" />

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

</manifest>

MainActivity.java 我使用这个任务来注册

In MainActivity.java I am using this task to register

private class Register extends AsyncTask<Void, Void, String>{

        String regID;

        @Override
        protected String doInBackground(Void... params) {
            try{
                regID = gcm.register("40441*******");

            }catch(Exception ex){
                regID = "Failed";
                //Log.d("", ex.getMessage());
                ex.printStackTrace();
            }
            return regID;
        }
        @Override
        protected void onPostExecute(String result) {
            etRegid.setText(result);
            Log.d("", result);
            super.onPostExecute(result);
        }

    }

博士presponse:

{
    "multicast_id": 7260653358308753695,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [{
        "message_id": "0:1422710889970517%ef663a58f9fd7ecd"
    }]
}

帮帮我!谢谢你。

Help me! Thanks.

请注意: GooglePlayServices 被安装在手机上也是如此。我没有得到任何错误,而登记。

Note: GooglePlayServices are installed in the phone as well. I get no error while registration.

推荐答案

请确保您有


  1. 签名构建正确的关键(开发与生产)

  2. 更新你的PHP脚本将消息发送到新的GCM注册ID

这篇关于WakefulBroadcastReceiver的onReceive不触发的Andr​​oid GCM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 01:40