搭建GCM项目——服务端和客户端(二)

现在先来完成GCM的客户端,首先创建android项目,名字就是之前在Google Console中注册的那个 DemoProject

创建完成之后,用Android SDK Manager确认你的SDK中已经下载了extras中的Google Cloud Messaging for Android Library已经安装,如果没有,请下载

之后在项目中导入gcm.jar,位置在:..\adt-bundle-windows-x86_64-20131030\sdk\extras\google\gcm\gcm-client\dist

现在开始可以着手写Service了

(1) 在Manifest文件夹定义的package中创建GCMIntentService (一定要是这个名字!!),详细代码如下


package com.example.pushproject;

import com.google.android.gcm.GCMBaseIntentService;

import android.R.anim;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

public class GCMIntentService  extends GCMBaseIntentService {
	public static final String SENDERID="";
	public GCMIntentService() {
		super(SENDERID);
	}

	@Override
	protected void onError(Context arg0, String arg1) {
        Log.v("GCMIntentService", "onError");  
	}

	@Override
	protected void onMessage(Context arg0, Intent arg1) {
        String titleString=arg1.getStringExtra("title");
        String contentString=arg1.getStringExtra("content");
        showNotification(titleString,contentString);
	}
	@Override
	protected void onRegistered(Context arg0, String arg1) {
        Log.v("GCMIntentService", "onRegistered success ");
	}

	@Override
	protected void onUnregistered(Context arg0, String arg1) {
        Log.v("GCMIntentService", "onUnregistered");  
	}
	public void showNotification(String title,String content){
		NotificationManager manager=(NotificationManager)this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
		Notification notification=new Notification(R.drawable.ic_launcher,title,System.currentTimeMillis());
	    notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中   
	    notification.flags |= Notification.FLAG_SHOW_LIGHTS;  
	    notification.defaults = Notification.DEFAULT_LIGHTS;
	    notification.ledARGB = Color.BLUE;  
	    notification.ledOnMS =5000; //闪光时间,毫秒
	          
     // 设置通知的事件消息  
        CharSequence contentTitle =title; // 通知栏标题  
	    CharSequence contentText =content; // 通知栏内容  
	    Intent notificationIntent =new Intent(); // 点击该通知后要跳转的Activity  
	    PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0);  
        notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);  
         
   // 把Notification传递给NotificationManager  
       manager.notify(0, notification);   
		
	}
}


其中SENDERID就是之前在GOOGLE console中申请的Project Number, 按照我们之前的例子,它应该是1023569414169

(2) 修改MainActivity

在MainActivity中,我们将注册好的Registration ID发送到服务器端存储起来,注意不要在主线程中使用HttpClient发送消息,应该写一个新的AsyncTask发送

另外,registration ID的注册可能存在一点点延迟现象,所以在这里使用定时器,确认返回ID后再发送,代码如下:


package com.example.pushproject;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.R.integer;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import com.google.android.gcm.GCMRegistrar;

public class MainActivity extends Activity {
	private String regIdString;
	private Button unButton;
	public static String accessUrl="http://请使用详细地址,使用127.0.0.1是发送不出去的:8080";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// Register
		GCMRegistrar.checkDevice(this);
		GCMRegistrar.checkManifest(this);
		regIdString = GCMRegistrar.getRegistrationId(this);
		if (regIdString.equals("")) {
			GCMRegistrar.register(this, GCMIntentService.SENDERID);
			// when register device to GCM, it got time delay. So we use Timer send registerID to our app server
			Timer timer=new Timer();
			timer.schedule(new GetRegistrationID(), 5000);
		}	
		// Test button, if click, you can remove registered and device can not
		// get notification any more
		unButton = (Button) findViewById(R.id.unreg);
		unButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				GCMRegistrar.unregister(getBaseContext());
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	class UpdateRegistrationDTask extends AsyncTask {
		@Override
		protected Object doInBackground(Object... arg0) {
			regIdString=arg0[0].toString();
			String urlString = accessUrl+"/DemoProject/GetDeviceId.do"
					+ "?deviceId=" + regIdString;
			HttpGet httpGet = new HttpGet(urlString);
			HttpClient httpClient = new DefaultHttpClient();
			try {
				HttpResponse response = httpClient.execute(httpGet);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
	}		
	class GetRegistrationID extends TimerTask{
		@Override
		public void run() {
			String testString=GCMRegistrar.getRegistrationId(getApplicationContext());
			new UpdateRegistrationDTask().execute(testString);
		}
		
	}
}

(3)修改Manifest.xml文件,添加许可,我的XML文件如下,仅供大家参考

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <permission  
        android:name="com.example.demoproject.permission.C2D_MESSAGE"  
        android:protectionLevel="signature"/>  
    <uses-permission  android:name="com.example.demoproject.permission.C2D_MESSAGE"/>  
    <uses-permission  android:name="com.google.android.c2dm.permission.RECEIVE"/>  
    <uses-permission  android:name="android.permission.INTERNET"/>  
    <uses-permission  android:name="android.permission.GET_ACCOUNTS"/>  
    <uses-permission  android:name="android.permission.WAKE_LOCK"/>  
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.pushproject.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.gcm.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.REGISTRATION"/>  
                <category  
                    android:name="com.example.demoproject"/>  
            </intent-filter>  
        </receiver>  
        <service
            android:name=".GCMIntentService">
        </service>
    </application>

</manifest>


下面是一些permission的说明

1. com.google.android.c2dm.permission.RECEIVE 使得Android app可以向GCM注册,且获得信息

2. android.permission.INTERNET 使得Android app可以向第三方服务器发送registration ID

3. android.permission.GET_ACCOUNTS 当Android系统低于4.0.4的时候,使得GCM获得用户的Google账户

4. android.permission.WAKE_LOCK 这是可选项,如果你希望你的app在sleeping状态下也能接收信息的话,请添加该permission

5. applicationPackage + ".permission.C2D_MESSAGE"   该permission防止别的app注册并获得推送消息。该permission必须严格遵守该规范,否则你的app将不会接收任何推送消息

6.       定义了com.google.android.c2dm.intent.RECEIVE 的receiver。 该receiver同时要定义com.google.android.c2dm.SEND 许可,这样就只有GCM框架可以给他发送消息。如果你的app使用IntentService,那么这个receiver必须是WakefulBroadcastReceiver的实例。

7. 最后确定你的app最低版本在8以上,如果想要使用GCM服务的话

注意,在permission和第一个uses-permission中,将其中的包名改为你们自己的,同时需要修改的还有receiver中的category

至此,Client部分结束


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言实现TLS1.3协议服务端客户端发送来支持的加密套件列表中选择双方都支持的加密套件的示例代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_CIPHER_SUITES 10 typedef struct { int id; char *name; } CipherSuite; CipherSuite cipherSuites[MAX_CIPHER_SUITES] = { { 0x1301, "TLS_AES_128_GCM_SHA256" }, { 0x1302, "TLS_AES_256_GCM_SHA384" }, { 0x1303, "TLS_CHACHA20_POLY1305_SHA256" }, { 0x1304, "TLS_AES_128_CCM_SHA256" }, { 0x1305, "TLS_AES_128_CCM_8_SHA256" } }; int main() { int clientSuites[MAX_CIPHER_SUITES] = { 0x1301, 0x1303, 0x1305 }; int serverSuites[MAX_CIPHER_SUITES] = { 0 }; int numClientSuites = 3; int numServerSuites = 0; int i, j; int selectedSuite = 0; // Find matching cipher suites for (i = 0; i < numClientSuites; i++) { for (j = 0; j < MAX_CIPHER_SUITES; j++) { if (clientSuites[i] == cipherSuites[j].id) { serverSuites[numServerSuites++] = clientSuites[i]; break; } } } // Select a cipher suite if (numServerSuites > 0) { selectedSuite = serverSuites[0]; } // Print selected cipher suite for (i = 0; i < MAX_CIPHER_SUITES; i++) { if (cipherSuites[i].id == selectedSuite) { printf("Selected cipher suite: %s (0x%04X)\n", cipherSuites[i].name, selectedSuite); break; } } return 0; } ``` 在这个示例代码中,我们假设客户端支持的加密套件列表已经存储在`clientSuites`数组中,而服务器支持的加密套件列表则存储在`serverSuites`数组中。通过遍历客户端支持的加密套件列表,我们可以找到服务器也支持的加密套件,将其添加到服务器支持的加密套件列表中。最后,我们选择服务器支持的加密套件列表中的第一个加密套件作为双方都支持的加密套件。在此示例代码中,我们选择了`TLS_AES_128_GCM_SHA256`加密套件作为双方都支持的加密套件。 请注意,本示例代码仅用于演示目的。在实际情况下,要选择适当的加密套件,您需要考虑多种因素,例如安全性、性能和互操作性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值