Android的GCM注册ID [英] Android GCM Registration Id
本文介绍了Android的GCM注册ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用GCM。我试着用在客户端如下:
清单:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android包=com.example.test机器人:INSTALLLOCATION =preferExternal安卓版code =1机器人:VERSIONNAME =1.0>
<使用-SDK安卓的minSdkVersion =8的Android版本:targetSdkVersion =16/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.ACCESS_NETWORK_STATE/>
<使用-权限的Android:名称=android.permission.ACCESS_WIFI_STATE/>
<使用-权限的Android:名称=android.permission.VIBRATE/>
<使用-权限的Android:名称=android.permission.GET_ACCOUNTS/>
<使用-权限的Android:名称=android.permission.WAKE_LOCK/>
<使用-权限的Android:名称=com.google.android.c2dm.permission.RECEIVE/>
<允许机器人:名称=com.example.test.permission.C2D_MESSAGE安卓的ProtectionLevel =签名/>
<使用-权限的Android:名称=com.example.test.permission.C2D_MESSAGE/>
<应用机器人:图标=@可绘制/ ic_launcher机器人:标签=@字符串/ APP_NAME>
<活动机器人:screenOrientation =画像
机器人:名称=。MainActivity
机器人:标签=@字符串/ APP_NAME
机器人:主题=@安卓风格/ Theme.NoTitleBar.Fullscreen>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<接收器的Android版本:NAME =com.google.android.gcm.GCMBroadcastReceiver安卓权限=com.google.android.c2dm.permission.SEND>
<意向滤光器>
<作用机器人:名称=com.google.android.c2dm.intent.RECEIVE/>
<作用机器人:名称=com.google.android.c2dm.intent.REGISTRATION/>
<类机器人:名称=com.example.test/>
&所述; /意图滤光器>
< /接收器>
<服务机器人。GCMIntentService名称=机器人:启用=真/>
< /用途>
< /舱单>
活动:
包com.example.test;
进口com.google.android.gcm.GCMRegistrar;
进口android.os.Bundle;
进口android.app.Activity;
进口android.app.PendingIntent;
进口android.content.Intent;
进口android.util.Log;
进口android.view.Menu;
进口android.view.WindowManager;
进口android.widget.Toast;
公共类MainActivity延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
gcmRelated();
}
私人无效gcmRelated(){
GCMRegistrar.checkDevice(本);
GCMRegistrar.checkManifest(本);
最后弦乐REGID = GCMRegistrar.getRegistrationId(本);
Toast.makeText(这一点,REGID,10000).show();
如果(regId.equals()){
GCMRegistrar.register(这一点,609612932495);
} 其他 {
Log.v(注册,已经注册);
}
}
}
GCMIntentService.java:
包com.example.test;
进口com.google.android.gcm.GCMBaseIntentService;
进口com.google.android.gcm.GCMRegistrar;
进口android.content.Context;
进口android.content.Intent;
进口android.os.PowerManager;
进口java.util.logging.Logger中;
公共类GCMIntentService扩展GCMBaseIntentService
{
私有静态PowerManager.WakeLock sWakeLock;
私有静态最终目标锁定= GCMIntentService.class;
私有静态最后弦乐GCM_SENDER_ID =609612932495;
私有静态最后弦乐GCM_INTENT_FILTER =com.example.test.GCM_MESSAGE;
私有静态最后弦乐MESSAGE_TYPE =类型;
私有静态最后弦乐MESSAGE_CONTENT =身体;
私有静态记录器记录= Logger.getLogger(GCMIntentService.class.getName());
公共GCMIntentService()
{
超(GCM_SENDER_ID);
}
静态无效runIntentInService(上下文的背景下,意图意图)
{
同步(LOCK)
{
如果(sWakeLock == NULL)
{
电源管理器PM =(电源管理器)context.getSystemService(Context.POWER_SERVICE);
sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCKsc_wakelock);
}
}
sWakeLock.acquire();
intent.setClassName(上下文,GCMIntentService.class.getName());
context.startService(意向);
}
@覆盖保护无效onRegistered(上下文的背景下,字符串registrationId)
{
log.warning(从GCMIntentService:设备成功注册为+ registrationId);
}
@覆盖保护无效onUnregistered(上下文的背景下,字符串registrationId)
{
log.warning(从GCMIntentService:设备未注册成功);
}
@覆盖保护无效的onMessage(上下文的背景下,意图messageIntent)
{
log.warning(从GCMIntentService:游戏更新公告收到);
}
@覆盖保护无效onDeletedMessages(上下文的背景下,INT总)
{
log.warning(从GCMIntentService:服务器中删除+ Integer.toString(总)+待处理消息);
}
@覆盖公共无效onerror的(上下文的背景下,字符串ErrorID中)
{
log.warning(从GCMIntentService:错误+ ErrorID中);
}
@覆盖保护布尔onRecoverableError(上下文的背景下,字符串ErrorID中)
{
log.warning(从GCMIntentService:可恢复错误+ ErrorID中);
返程(super.onRecoverableError(背景下,ErrorID中));
}
}
它显示出任何错误。 GCMRegistrar.getRegistrationId(本)总是返回(空字符串)。因此,它总是试图注册和我从来没有registerId。
有什么要做些什么呢?
此外,在实施服务器端code,我需要发送这个寄存器ID,并保存所有的ID在数据库中,并给他们?
解决方案
公共类GCMIntentService扩展GCMBaseIntentService {
@燮pressWarnings(未使用)
私人最终字符串变量=GCMIntentService;
公共GCMIntentService(){
超级(GCM_ID);
}
/ **
*问题的通知,通知服务器发送邮件的用户。
* /
公共无效generateNotification(上下文的背景下,字符串消息){
时长= System.currentTimeMillis的();
NotificationManager notificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
通知通知=新的通知(R.drawable.ic_launcher,消息的时候);
字符串标题= context.getString(R.string.app_name);
意图notificationIntent =新的意图(背景下,SamplePushActivity.class);
PendingIntent意图= PendingIntent.getActivity(上下文,0,notificationIntent,android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.setLatestEventInfo(背景下,标题,邮件,意图);
notification.flags | = Notification.FLAG_AUTO_CANCEL;
notification.defaults | = Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS;
notificationManager.notify(0,通知);
}
@覆盖
保护无效的onError(背景为arg0,串ARG1){
}
@覆盖
保护无效的onMessage(背景为arg0,意图ARG1){
资源RES = getResources();
Log.d(GCM,收到了一条消息);
//从意图获取数据并发送至notificaion吧
的System.out.println(消息:收到:+ arg1.getStringExtra(信息));
的System.out.println(数据:收到:+ arg1.getExtras()的getString(collapse_key)。);
// generateNotification(为arg0,arg1.getStringExtra(国家));
// generateNotification(为arg0,arg1.getStringExtra(信息));
generateNotification(为arg0,res.getString(R.string.assignment_new,ARG1,ARG2));
//串notifyMessage = arg1.getStringExtra(collapse_key);
// 如果
//(arg1.getStringExtra(collapse_key)。equalsIgnoreCase(NOTIFICATION_ASSIGNMENT))
// {
// generateNotification(为arg0,arg1.getStringExtra(信息));
//}
}
@覆盖
保护无效onRegistered(背景为arg0,串ARG1){
// TODO自动生成方法存根
}
@覆盖
保护无效onUnregistered(背景为arg0,串ARG1){
// TODO自动生成方法存根
}
公共类SamplePushActivity延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
TextView的regNumber =(TextView中)findViewById(R.id.textView1);
GCMRegistrar.checkDevice(本);
GCMRegistrar.checkManifest(本);
如果(GCMRegistrar.isRegistered(本)){
Log.d(信息,GCMRegistrar.getRegistrationId(本));
}
最后弦乐REGID = GCMRegistrar.getRegistrationId(本);
regNumber.setText(REGID);
如果(regId.equals()){
//与项目ID替换此
的System.out.println(注册ID为+ REGID);
GCMRegistrar.register(这一点,GCM_ID);
Log.d(信息,GCMRegistrar.getRegistrationId(本));
的System.out.println(获取注册号:+ GCMRegistrar.getRegistrationId(本));
regNumber.setText(REGID);
} 其他 {
Log.d(信息,已注册为+ REGID);
}
}
<使用-SDK安卓的minSdkVersion =8/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.GET_ACCOUNTS/>
<使用-权限的Android:名称=android.permission.WAKE_LOCK/>
<许可
机器人:名称=in.android.gcm.permission.C2D_MESSAGE
安卓的ProtectionLevel =签名/>
<使用-权限的Android:名称=com.google.android.c2dm.permission.RECEIVE/>
<使用-权限的Android:名称=in.android.gcm.permission.C2D_MESSAGE/>
<使用-权限的Android:名称=android.permission.GET_ACCOUNTS/>
<使用-权限的Android:名称=android.permission.USE_CREDENTIALS/>
<应用
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME>
<活动
机器人:名称=。SamplePushActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<接收器
机器人:名称=com.google.android.gcm.GCMBroadcastReceiver
机器人:权限=com.google.android.c2dm.permission.SEND>
<意向滤光器>
<作用机器人:名称=com.google.android.c2dm.intent.RECEIVE/>
<作用机器人:名称=com.google.android.c2dm.intent.REGISTRATION/>
<类机器人:名称=in.android.gcm/>
&所述; /意图滤光器>
< /接收器>
<服务机器人。GCMIntentServiceNAME = />
< /用途>
I want to use GCM. I tried with following in the client side:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test" android:installLocation="preferExternal" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<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.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.test.permission.C2D_MESSAGE" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
<activity android:screenOrientation="portrait"
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<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.test" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" android:enabled="true" />
</application>
</manifest>
Activity:
package com.example.test;
import com.google.android.gcm.GCMRegistrar;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gcmRelated();
}
private void gcmRelated() {
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
Toast.makeText(this, regId, 10000).show();
if (regId.equals("")) {
GCMRegistrar.register(this, "609612932495");
} else {
Log.v("Register", "Already registered");
}
}
}
GCMIntentService.java:
package com.example.test;
import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMRegistrar;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import java.util.logging.Logger;
public class GCMIntentService extends GCMBaseIntentService
{
private static PowerManager.WakeLock sWakeLock;
private static final Object LOCK = GCMIntentService.class;
private static final String GCM_SENDER_ID = "609612932495";
private static final String GCM_INTENT_FILTER = "com.example.test.GCM_MESSAGE";
private static final String MESSAGE_TYPE = "Type";
private static final String MESSAGE_CONTENT = "Body";
private static Logger log = Logger.getLogger(GCMIntentService.class.getName());
public GCMIntentService()
{
super(GCM_SENDER_ID);
}
static void runIntentInService(Context context,Intent intent)
{
synchronized(LOCK)
{
if (sWakeLock == null)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"sc_wakelock");
}
}
sWakeLock.acquire();
intent.setClassName(context,GCMIntentService.class.getName());
context.startService(intent);
}
@Override protected void onRegistered(Context context,String registrationId)
{
log.warning("From GCMIntentService: Device successfully registered as "+registrationId);
}
@Override protected void onUnregistered(Context context,String registrationId)
{
log.warning("From GCMIntentService: Device successfully unregistered");
}
@Override protected void onMessage(Context context,Intent messageIntent)
{
log.warning("From GCMIntentService: Game update notice received");
}
@Override protected void onDeletedMessages(Context context,int total)
{
log.warning("From GCMIntentService: Server deleted "+Integer.toString(total)+" pending messages");
}
@Override public void onError(Context context,String errorId)
{
log.warning("From GCMIntentService: Error "+errorId);
}
@Override protected boolean onRecoverableError(Context context,String errorId)
{
log.warning("From GCMIntentService: Recoverable error "+errorId);
return(super.onRecoverableError(context,errorId));
}
}
It's showing no error. GCMRegistrar.getRegistrationId(this) is always returning " "(empty string). So, It always tries to register and I never got registerId.
Anything to be done?
Also, While implementing the server side code, Do I need to send this register Id and save all the id's in a database and send them?
解决方案
public class GCMIntentService extends GCMBaseIntentService {
@SuppressWarnings("unused")
private final String TAG = "GCMIntentService";
public GCMIntentService() {
super("GCM_ID");
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
public void generateNotification(Context context, String message) {
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, SamplePushActivity.class);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS;
notificationManager.notify(0, notification);
}
@Override
protected void onError(Context arg0, String arg1) {
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
Resources res = getResources();
Log.d("GCM", "RECIEVED A MESSAGE");
// Get the data from intent and send to notificaion bar
System.out.println("MEssage Recieved :" + arg1.getStringExtra("message"));
System.out.println("Data Recieved :" + arg1.getExtras().getString("collapse_key"));
// generateNotification(arg0, arg1.getStringExtra("state"));
// generateNotification(arg0, arg1.getStringExtra("message"));
generateNotification(arg0, res.getString(R.string.assignment_new, "arg1", "arg2"));
// String notifyMessage = arg1.getStringExtra("collapse_key");
// if
// (arg1.getStringExtra("collapse_key").equalsIgnoreCase("NOTIFICATION_ASSIGNMENT"))
// {
// generateNotification(arg0, arg1.getStringExtra("message"));
// }
}
@Override
protected void onRegistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
public class SamplePushActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView regNumber = (TextView) findViewById(R.id.textView1);
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
if (GCMRegistrar.isRegistered(this)) {
Log.d("info", GCMRegistrar.getRegistrationId(this));
}
final String regId = GCMRegistrar.getRegistrationId(this);
regNumber.setText(regId);
if (regId.equals("")) {
// replace this with the project ID
System.out.println("registration ID is " + regId);
GCMRegistrar.register(this, "GCM_ID");
Log.d("info", GCMRegistrar.getRegistrationId(this));
System.out.println("Get Reg ID: " + GCMRegistrar.getRegistrationId(this));
regNumber.setText(regId);
} else {
Log.d("info", "already registered as" + regId);
}
}
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="in.android.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="in.android.gcm.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SamplePushActivity"
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="in.android.gcm" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
这篇关于Android的GCM注册ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文