Android GCM push通知


首先Application导入GCM有8个步骤

1.Google Developer Console 项目的作成, GCM for Android有效化
2.Google Developers 设定文件 (google-services.json 取得)
3.将google-services.json 放入项目的app文件根目录下
4.build.gradle Google Play Service的追加
5.AndroidMainfest.xml 的permission 等追加
6.新建类,继承IntentService,并在其中取得Registration token
7.新建类,继承GcmListenerService,并在其中添加Push接收时的处理
8.新建类,继承InstanceIDListenerService,并在其中添加Registration token 变更时的处理


1.Google Developer Console项目的作成, GCM for Android有效化

参照官网


2.Google Developers的设定,取得google-services.json

https://developers.google.com/cloud-messaging/android/client

点击GET A CONFIGURATION FILE 按钮,输入AppName(可随便取)和Package Name(与App包名一致)
.....
最后Download google-services.json


3.在Android Studio中,配置json文件

API Level10以上,将刚才下载的google-services.json文件放进app文件夹的根目录下


4.build.gradle的编辑

1)  在build.gradle(Project:xxx) 中 添加 google services
如 classpath 'com.google.gms:google-services:1.4.0-beta3'

buildscript {
		    repositories {
		        jcenter()
		    }
		    dependencies {
		        classpath 'com.android.tools.build:gradle:1.3.0'
		        classpath 'com.google.gms:google-services:1.4.0-beta3'

		        // NOTE: Do not place your application dependencies here; they belong
		        // in the individual module build.gradle files
		    }
		}


2)  在build.gradle(Module:app) 中添加 dependencies
   如 compile 'com.google.android.gms:play-services:8.1.0'

dependencies {
		    compile fileTree(dir: 'libs', include: ['*.jar'])
		    compile 'com.android.support:appcompat-v7:22.2.1'

		    compile 'com.google.android.gms:play-services:8.1.0'
		}


5.AndroidManifest.xml的编辑

一些必需的permission添加,{package}部分请用app的包名替换

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	    package="{package}" >

	    <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="{package}.permission.C2D_MESSAGE"
	                android:protectionLevel="signature" />
	    <uses-permission android:name="{package}.permission.C2D_MESSAGE" />

	    <application>
	        <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="{package}" />
	            </intent-filter>
	            <intent-filter>
	                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
	                <category android:name="{package}" />
	            </intent-filter>
	        </receiver>
	    </application>
	</manifest>

6.新建类RegistrationIntentService,继承IntentService类

public class RegistrationIntentService extends IntentService {
	    private static final String TAG = "Registration service";

	    /**
	     * Creates an IntentService.  Invoked by your subclass's constructor.
	     */
	    public RegistrationIntentService() {
	        super("Registration service");
	    }

	    @Override
	    protected void onHandleIntent(Intent intent) {
	        try {
	            InstanceID instanceID = InstanceID.getInstance(this);
	            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
	                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
	            Log.v(TAG, "token=" + token);
	            // 获取token 用于 push
	        } catch (IOException e) {
	            Log.v(TAG, "IO Exception " + e.getMessage());
	        }
	    }
	}
AndroidManifest.xml的追加

<manifest>
	    <application>
	        <service android:name=".RegistrationIntentService"/>
	    </application>
	</manifest>

7.新建类MyGCMListenerservice,继承GcmListenerService类

Google Play Services 7.5 以前使用BroadcastReceiver,之后使用GcmListenerService
Push Message 接收时,调用onMessageReceived(), 第一个参数是Sender ID, 第二个参数是push的数据

public class MyGCMListenerservice extends GcmListenerService {
	    private static final String TAG = "GCMListener";

	    @Override
	    public void onMessageReceived(String from, Bundle data) {
	        super.onMessageReceived(from, data);
	        Log.v(TAG, "from=" + from);
	        Log.v(TAG, "data=" + data.toString());
	    }
	}
AndroidManifest.xml的追加

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

8.新建类MyInstanceIDListenerService,继承InstanceIDListenerService类

GCM的registration token不定期的变更,InstanceIDListenerService添加了变更时的处理

public class MyInstanceIDListenerService extends InstanceIDListenerService {
	    @Override
	    public void onTokenRefresh() {
	        super.onTokenRefresh();

	    }
	}
AndroidManifest.xml的追加

<manifest>
	    <application>
	        <service
	            android:name=".MyInstanceIDListenerService"
	            android:exported="false">
	            <intent-filter>
	                <action android:name="com.google.android.gms.iid.InstanceID"/>
	            </intent-filter>
	        </service>
	    </application>
	</manifest>

AndroidManifest.xml最后的样子

//app的包名为com.mokelab.demo.gcm
	<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	    package="com.mokelab.demo.gcm" >

	    <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="com.mokelab.demo.gcm.permission.C2D_MESSAGE"
	                android:protectionLevel="signature" />
	    <uses-permission android:name="com.mokelab.demo.gcm.permission.C2D_MESSAGE" />


	    <application
	        android:allowBackup="true"
	        android:icon="@mipmap/ic_launcher"
	        android:label="@string/app_name"
	        android:theme="@style/AppTheme" >
	        <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="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="com.mokelab.demo.gcm" />
	            </intent-filter>
	            <intent-filter>
	                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
	                <category android:name="com.mokelab.demo.gcm" />
	            </intent-filter>
	        </receiver>
	        <service
	            android:name=".MyGCMListenerservice"
	            android:exported="false" >
	            <intent-filter>
	                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
	            </intent-filter>
	        </service>
	        <service
	            android:name=".MyInstanceIDListenerService"
	            android:exported="false">
	            <intent-filter>
	                <action android:name="com.google.android.gms.iid.InstanceID"/>
	            </intent-filter>
	        </service>

	        <service android:name=".RegistrationIntentService"/>
	    </application>

	</manifest>


GCM linux push

curl --header "Authorization: key=AIzaSyB3L2ti-_EYzCQGQC2ZJc0KhLe6oH8bq9I" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"eNjyAHOKNiI:APA91bE4kJR2-kLom8DnHZjrDZ9uxXS2u0evNG_gVsLC8LQoOtLF3bd7WPOnbeFox1dgDc86ZIyr_7fYrai_CERsfaMM1dDgJwXZ8HwDS6hot2ReByTo6QhzDPwgjid9jZQ8BibzH5dW\"],\"data\":{\"message\":\"Hello\",\"type\":\"test\"}}"

mac上终端上运行这段代码,就可以发送push通知给App,必须有vpn, 蓝色部分的代码为替换区域

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值