Getting Registration id value as null from the following code to implement C2DM using Android. ,Can anyone pls tell me what mistake am doing here ?
public void StartRegistrationNotification()
{
Intent registrationIntent = newIntent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.setPackage("com.google.android.gsf");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
registrationIntent.putExtra("sender", "kanika.saya@gmail.com");
this.startService(registrationIntent);
Log.i("Recieve","1");
}
public class ReceiverC2DM extends android.content.BroadcastReceiver
{
private static String KEY = "c2dmPref";
private static String REGISTRATION_KEY = "registraionKey";
private Context context;
// wakelock
private static final String WAKELOCK_KEY = "C2DM_FAX";
private static PowerManager.WakeLock mWakeLock;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
this.context = context;
Toast.makeText(context,"At broadcast receiver", Toast.LENGTH_LONG).show();
runIntentInService(context, intent);
Toast.makeText(context,"runIntentInService", Toast.LENGTH_LONG).show();
if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
handleRegistration(context, intent);
} else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
handleMessage(context, intent);
}
}
private void handleRegistration(Context context, Intent intent) {
String registration = intent.getStringExtra("registration_key");
Log.e("registration :","registration :"+registration);
Toast.makeText(context,"registration :"+registration, Toast.LENGTH_LONG).show();
if (intent.getStringExtra("error") != null) {
// Registration failed, should try again later.
Log.d("c2dm", "registration failed");
String error = intent.getStringExtra("error");
if(error == "SERVICE_NOT_AVAILABLE"){
Toast.makeText(context,"Service not available", Toast.LENGTH_LONG).show();
Log.d("c2dm", "SERVICE_NOT_AVAILABLE");
}
else if(error == "ACCOUNT_MISSING"){
Toast.makeText(context,"Account Missing", Toast.LENGTH_LONG).show();
Log.d("c2dm", "ACCOUNT_MISSING");
}else if(error == "AUTHENTICATION_FAILED"){
Toast.makeText(context,"Authentication failed", Toast.LENGTH_LONG).show();
Log.d("c2dm", "AUTHENTICATION_FAILED");
}else if(error == "TOO_MANY_REGISTRATIONS"){
Toast.makeText(context,"too many registration", Toast.LENGTH_LONG).show();
Log.d("c2dm", "TOO_MANY_REGISTRATIONS");
}else if(error == "INVALID_SENDER"){
Toast.makeText(context,"invalid sender", Toast.LENGTH_LONG).show();
Log.d("c2dm", "INVALID_SENDER");
}else if(error == "PHONE_REGISTRATION_ERROR"){
Log.d("c2dm", "PHONE_REGISTRATION_ERROR");
}
} else if (intent.getStringExtra("unregistered") != null) {
// unregistration done, new messages from the authorized sender will be rejected
Log.d("c2dm", "unregistered");
} else if (registration != null) {
Log.d("c2dm", registration);
Toast.makeText(context,"registration!=null="+registration, Toast.LENGTH_LONG).show();
Editor editor =
context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(REGISTRATION_KEY, registration);
Toast.makeText(context,"registration :"+registration, Toast.LENGTH_LONG).show();
editor.commit();
// Send the registration ID to the 3rd party site that is sending the messages.
// This should be done in a separate thread.
// When done, remember that all registration is done.
}
}
private void handleMessage(Context context, Intent intent)
{
String message = intent.getExtras().getString("payload");
String key = intent.getExtras().getString("collapse_key");
/*Log.e("","accountName : " +accountName);
Log.e("","message : " +message);
开发者_开发问答 Intent startActivity = new Intent();
startActivity.setClass(context, NotificationAlert.class);
startActivity.setAction(NotificationAlert.class.getName());
startActivity.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity.putExtra("Title", "Hello");
startActivity.putExtra("Message", message);
context.startActivity(startActivity);
*/
//Do whatever you want with the message
}
static void runIntentInService(Context context, Intent intent) {
if (mWakeLock == null) {
// This is called from BroadcastReceiver, there is no init.
PowerManager pm =
(PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
WAKELOCK_KEY);
}
mWakeLock.acquire();
}
}
Manifest file is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.synapse.reference"
android:installLocation="auto"
android:versionCode="1"
android:versionName="2.2">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11" />
<permission android:name="com.synapse.reference.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.synapse.reference.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive message -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Send the registration id to the server -->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-permission android:name="android.permission.INTERNET"></uses-permission>
<activity android:name=".SplashScreen"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainMenuScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ServerSimulator"/>
<activity android:name=".Information"/>
<activity android:name=".About"/>
<receiver android:name=".ReceiverC2DM"
android:permission="com.google.android.c2dm.permission.SEND">
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.synapse.reference" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.synapse.reference" />
</intent-filter>
</receiver>
</application>
</manifest>
private void handleRegistration(Context context, Intent intent) {
String registration = intent.getStringExtra("registration_key");
should actually be
String registration = intent.getStringExtra("registration_id");
Let me know if that works.
精彩评论