Using GCM, Urban Airship to send Push Notification

旧城冷巷雨未停 提交于 2019-12-13 01:32:43

问题


I have created a sample project in Google Console. Referring the sample from this site, http://code.google.com/p/blog-samples/downloads/detail?name=gcm_sample_update1.rar&can=2&q=#makechanges I got my registration Id. Now I don't know what to do next, how I can implement push notification in my app. I want to use Urban Airship to send push to my app. I have created a app in Urban Airship, by giving the package name, API key obtained through Google console. To my server I need to send the Apid to get the messages. But i don't know how to get the Apid.


回答1:


It is explained here: http://developer.android.com/guide/google/gcm/gs.html Writing the Android Application You have to download and add gcm libraries. Then make changes in manifest:

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

and

<permission android:name="my_app_package.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="my_app_package.permission.C2D_MESSAGE" /> 

inserting Your package name in "my_app_package". Add other permissions

<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" /> 
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

receiver

<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="my_app_package" />
</intent-filter>
</receiver>

Again write Your package name in "my_app_package". Then declare your service

<service android:name=".GCMIntentService" />

You have to create a class named GCMIntentService. This class is going to handle the push notifications. It should look like this:

class GCMIntentService extends  com.google.android.gcm.GCMBaseIntentService{
     public void onRegistered(Context context, String regId){}
     public void onUnregistered(Context context, String regId){}
     public void onError(Context context, String errorId){}
     public void onMessage(Context context, Intent intent){}
}

In onMessage() you will have to handle the push message. You can use notifications.

Then in Your starting Activity you will have to put this

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
  GCMRegistrar.register(this, SENDER_ID);
} else {
  Log.v(TAG, "Already registered");
}

in onCreate(). SENDER_ID is the String containing all numbers that you got from the google console.




回答2:


Well.. There are 3 steps to get APID 1. Run the application 2. Open logcat 3. Find the APID

Note first time you may get APID(Android Push ID) null so run it again you will get




回答3:


If you want to use Urbanairship you have to use their SDK for user registration (look here), cause UA create a new id for each device and not using Google registration Id when pushing message using their API.

Not sure why they doing it like that but that it... =\



来源:https://stackoverflow.com/questions/12787354/using-gcm-urban-airship-to-send-push-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!