I have one question about C2DM, I registered yesterday and I got email that my mail and app are approved. From app I get registration_id, I have broadcast_receiver like
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
android.util.Log.d("REG_1","onReceive");
if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
final String registrationId = intent
.getStringExtra("registration_id");
String error = intent.getStringExtra("error");
android.util.Log.d("REG_1",registrationId);
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
String token = prefs.getString("token", null);
String userId;
try {
userId = RestClient.getUserIdByToken(token).getString(
"user_id");
Intent i = new Intent(context, RegService.class);
i.putExtra("c2dm_registration_id",registrationId);
i.putExtra("token", token);
i.putExtra("user_id", userId);
i.putExtra("device_id", "bla");
i.setAction(android.content.Intent.ACTION_VIEW);
context.startService(i);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
saveRegistrationId(context, registrationId);
}
and I enter in this function during registration and I send messages from command line like
curl https://www.google.com/accounts/ClientLogin -d Email=MYGOOGLEACCOUNT#gmail.com -d "Passwd=MY_PASSWORD" -d accountType=GOOGLE -d source=Google-cURL-Example -d service=ac2dm
and
curl --header "Authorization: GoogleLogin auth=DQAAAMMAAAC58D4X-5zjQFdYuGz7D9DhnuN4OUiz_gCtOJRSNwNLN0-wxveAEVL985hNKJXyQ_7U4sTfsUGh_3OXMLKpB5PNN1eaI4AfT19LaJ1vGJCZ_sSE0NDqGsC0mZVdMsYbE2Sz1r1WE_p5WNokfGMRdmxIHl0QCWb43lTD3iCvr51ujmnHnvpn2mDLWr6j9DtyDxADRw1to2iGgpJNelXmIA8tOzjyqF3szN-N2IYnihJ8H2t3G5wotOWy1EahB43Lv2NPdlV-A4yVSbdsYGM_AVdd" "https://android.apis.google.com/c2dm/send" -d registration_id=APA91bHhbsPedDVnYCaSJQMhWjfjK3W9jOaMgVITUHqw97w4fF_8fermSG22CzFvpPuTyRKnJFyJ_iwfgJEJ4uidURxuHZCCBuPtGAsv6NeVipmOd53Fkru_A3NW3cpIMo9gvuVxIB0QqxOvl1SmVfqRzD4qQfSNaw -d "data.test_result=This data will be send to your application as payload" -d collapse_key=2
id=0:1322216144957968%b3c4048a00000032
but it never enter in onHandle function of broadcatreceiver.
<receiver
android:name="com.surveyce.android.c2dm.C2DMRegistrationReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter >
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.surveyce.android" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter >
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.surveyce.android" />
</intent-filter>
</receiver>
...
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!--<uses-permission android:name="android.permission.WAKE_LOCK" />-->
<uses-permission android:name="com.surveyce.android.permission.C2D_MESSAGE" />
<permission
android:name="com.surveyce.android.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Did anybody have similar strange problems that get registrationId and send messages but do not receive message on phone ? What can be problem, maybe because I registered yesterday ( but how than I get rigistrationId and auth for that account) ? Package names and gmail are account are 100% ok.
Add a service tag [write your receiver name in below service tag which is sub class of C2DMBaseReceiver]
<service android:name=".C2DMReceiver" />
Use C2DMBroadcastReceiver Instead of C2DMRegistrationReceiver
<receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
You can refer following Link for proper implementation of C2DM Google C2DM service
Related
I've configured Push notifications in Android for an app following all the instructions in:
https://github.com/ParsePlatform/Parse-Server/wiki/Push-Configuring-Clients
https://parseplatform.github.io/docs/android/guide/#push-notifications
But no matter what I do, the mobile phone I'm using can't seem to get any push notifications at all.
The same app is configured to receive the notifications on iOS and works perfectly.
These are the relevant bits in the Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxy.xxy">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<permission
android:name="com.xxy.xxy.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.xxy.xxy.permission.C2D_MESSAGE" />
<application
android:name="com.xxy.xxy.MiXXYApplication"
android:allowBackup="true"
android:icon="#drawable/logoapp"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<service android:name="com.parse.PushService" />
<receiver
android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.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.xxy.xxy" />
</intent-filter>
</receiver>
<meta-data
android:name="com.parse.push.gcm_sender_id"
android:value="id:123456789012" />
</application>
This is the Application class (with the AppID and Client Key replaced with the current value given by Back4App):
public class MiXXYApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);
Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
.applicationId("back4App_AppID")
.clientKey("back4App_ClientKey")
.server("https://parseapi.back4app.com/")
.build()
);
}
}
These are the methods I call to register the installation (I can't do it on the Application class because I need the userId, after login)
public void enablePush(final String userId) {
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put("GCMSenderId", "123456789012");
installation.put("userId", userId);
installation.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.i("push", "ok");
subscribe("");
subscribe(userId);
} else {
Log.i("push", "nok");
e.printStackTrace();
}
}
});
}
public void subscribe(String channel){
ParsePush.subscribeInBackground(channel, new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.i("Push", "subscribed");
} else {
Log.i("push", "nok");
e.printStackTrace();
}
}
});
}
This is the installation registry on the Parse DB:
ObjectID: l1tkO3YM2r
GCMSenderID: 123456789012
deviceToken: APA91bHeKnj...
localeIdentifier: en-US
badge: (undefined)
parseVersion: 1.13.1
ACL: Public Read + Write
appIdentifier: com.xxy.xxy
appName: appName
deviceType: android
channels: ["","CyTPw5xc4r"]
pushType: gcm
installationId: 8cf9c606-5a0e...
userId: CyTPw5xc4r
In the google developer console, the project number is 123456789012, I've created an API key and added the API key + the project number in the Back4App dashboard's Android Push Notification Settings.
The logcat in verbose mode is showing the following:
09-21 09:29:19.863 10721-10721/com.xxy.xxy V/com.parse.ManifestInfo: Using gcm for push.
09-21 09:29:19.884 10721-10885/com.xxy.xxy V/com.parse.CachedCurrentInstallationController: Successfully deserialized Installation object
09-21 09:29:19.899 10721-10886/com.xxy.xxy V/com.parse.GcmRegistrar: Sending GCM registration intent
09-21 09:29:29.782 10721-11340/com.xxy.xxy V/com.parse.GcmRegistrar: Received deviceToken <APA91bHeKnj...> from GCM.
For what I can tell, everything's good to go, but I'm not getting any notification in my phone.
Is there anything wrong with my setup?
Thanks!
Solved it!
The thing is that, instead of getting the Api key the way all the guides say, you should get it from https://developers.google.com/mobile/add.
It generated another API key and activated the service, and then, it worked.
I want to send push Notification using Parse API and GCM. I have done successfully configuration on server and test via sending push notification form Parse terminal and receive at android device.
But when i'm sending push programmatically then got an Exception: "unauthorized: master key is required".
I using following code:
ParseQuery query = ParseInstallation.getQuery();
query.whereEqualTo("channels", "testing");
// Notification for Android users
query.whereEqualTo("deviceType", "android");
ParsePush androidPush = new ParsePush();
androidPush.setMessage("Your suitcase has been filled with tiny robots!");
androidPush.setQuery(query);
androidPush.sendInBackground(new SendCallback() {
#Override
public void done(ParseException e) {
if ( e == null ) {
Log.d("GARG","Parse Notification Done. : ");
} else {
Log.d("GARG","Notification failed.: "+e.getMessage());
e.printStackTrace();
}
}
});
android manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo">
<permission
android:name="com.demo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.demo.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.demo.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.demo.permission.C2D_MESSAGE" />
<application
android:name=".AppInitializer"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<service android:name="com.parse.PushService" />
<meta-data
android:name="com.parse.push.notification_icon"
android:resource="#drawable/ic_cast_light" />
<meta-data android:name="com.parse.push.gcm_sender_id"
android:value="id:211671060483" />
<meta-data
android:name="com.parse.APPLICATION_ID"
android:value="#string/parse_app_id" />
<meta-data
android:name="com.parse.CLIENT_KEY"
android:value="#string/parse_client_key" />
<meta-data
android:name="com.parse.X-Parse-Master-Key"
android:value="#string/parse_master_key" />
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.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.demo" />
</intent-filter>
</receiver>
</application>
Initialize Parse SDK in android
public class AppInitializer extends Application {
public static JSONObject errorDataProvider;
#Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(getApplicationContext());
Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
.applicationId(Utility.ApplicationId)
.clientKey(Utility.ClientKey)
.server(Utility.Server_URL)
.build()
);
ParseInstallation.getCurrentInstallation().saveInBackground();
ParsePush.subscribeInBackground(Utility.PARSE_CHANNEL, new SaveCallback() {
#Override
public void done(ParseException e) {
Log.e("GARG", "Successfully subscribed to Parse!");
}
});
}
}
UPDATED ANSWER
Hi, i investigate it a bit and found that currently the only to send Push is by using the masterKey and that's exactly the reason why you are getting this error
In order to send push with master key the best approach will be to create a cloud code function and trigger this function from the client side.
so you need to do the following:
Inside your cloud code main.js file create a new function
Parse.Cloud.afterSave("SendPush", function(request) {
var query = new Parse.Query(Parse.Installation);
query.exists("deviceToken");
// here you can add other conditions e.g. to send a push to sepcific users or channel etc.
var payload = {
alert: "YOUR_MESSAGE"
// you can add other stuff here...
};
Parse.Push.send({
data: payload,
where: query
}, {
useMasterKey: true
})
.then(function() {
response.success("Push Sent!");
}, function(error) {
response.error("Error while trying to send push " + error.message);
});
});
After you created the cloud code function restart your server
Trigger this cloud code function from your android app in the following way:
HashMap<String,String> map = new HashMap<String, String>();
map.put("PARAM1KEY","PARAM1VALUE");
// here you can send parameters to your cloud code functions
// such parameters can be the channel name, array of users to send a push to and more...
ParseCloud.callFunctionInBackground("SendPush",map, new FunctionCallback<Object>() {
#Override
public void done(Object object, ParseException e) {
// handle callback
}
});
This will trigger a call to the cloud code function that you created above and inside the cloud code the useMasterKey is true so it should work.
update: spelling
useMasterKey: true
that will enable parse server to use master key to push notification from client side.
I am trying to implement parse push notification in my android project. to understand, I am giving some project flow.
saving user details on personal server using php API
server send me some push notification.
I get notification but I am not getting any action, data in that push notification in my custom broadcast receiver.
Here is my Android Manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CAMERA" />
<permission
android:name="com.mypackage.myapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.mypackage.myapp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:name="com.mypackage.myappe.MyApplication"
android:allowBackup="true"
android:icon="#drawable/dwtlogo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service android:name="com.parse.PushService" />
<receiver
android:name="com.mypackage.myapp.MyCustomReceiver"
android:exported="false" android:enabled="true">
<intent-filter>
<action android:name="com.mypackage.myapp.MESSAGE" />
</intent-filter>
</receiver>
<category android:name="com.mypackage.myapp" />
</application>
Here is MyApplication.java
#Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, "xxxxxxxx", "xxxxxxxxxx");
ParseACL defaultACL = new ParseACL();
defaultACL.setPublicReadAccess(true);
PushService.setDefaultPushCallback(getApplicationContext(), DashBoardActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
ParseUser.enableAutomaticUser();
//String deviceToken= (String) installation.get("deviceToken");
//Toast.makeText(this, "Device token="+deviceToken, Toast.LENGTH_LONG).show();
PushService.subscribe(this, "Wake_up", DashBoardActivity.class);
}
And here is my custom receiver in which i am trying to get action and data which server sending me but my custom receiver not getting called.
String action = intent.getAction();
Log.d(TAG, "got action " + action );
if (action.equals("com.mypackage.myapp.MESSAGE"))
{
String channel = intent.getExtras().getString("com.parse.Channel");
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
Iterator itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
//if (key.equals("customdata"))
//{
Intent pupInt = new Intent(context, ShowPopup.class);
pupInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.getApplicationContext().startActivity(pupInt);
//}
Log.d(TAG, "..." + key + " => " + json.getString(key));
}
}
}
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
Even no any log getting printed. Here is php server code which sending me push notification.
$data = array(
'channel' => $chnl,
'action' => 'com.mypackage.myapp.MESSAGE',
'data' => array(
'alert' => $message,
'sound' => 'push.caf',
'photourl' => $imageurl,
)
);
But i don't understand where i need to get this data so i can show image after receiving push notification.
Please give me any hint or reference.
Adding receiver for GcmBroadcastReceiver should help. As given out in the tutorial here.
Other reference.
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.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" />
<!--
IMPORTANT: Change "com.parse.tutorials.pushnotifications" to match your app's package name.
-->
<category android:name="com.parse.tutorials.pushnotifications" />
</intent-filter>
</receiver>
I'm trying to register my android app with GCM, I followed the instructions in this link in order to obtain an api key, I did what is written in numbers 1-7 but in step 8 I can't find
the Android Key section.
What should I do?
p.s.
When I try to register my device with gcm gcm.register(SENDER_ID) I get error saying service not available, are those to problems connected? or this error is something else? (I didn't write the server part of the application)
android manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appspot.smartgan"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.appspot.smartgan.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.appspot.smartgan.permission.C2D_MESSAGE" />
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
<activity
android:name="com.appspot.smartgan.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=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.appspot.smartgan" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="com.appspot.smartgan.LoginActivity"
android:label="#string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateVisible" >
</activity>
<activity
android:name="com.appspot.smartgan.ChildActivity"
android:label="#string/title_activity_child" >
</activity>
</application>
</manifest>
registration code: (I used the code from an example Google published)
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
String str = "";
#Override
protected String doInBackground(Void... params) {
String message = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
message = "Device registered, registration ID=" + regid;
Log.d("SMARTGAN_PLAY", "register completed");
// send registration id to the server
sendRegistrationIdToServer();
// Persist the regID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException e) {
message = "Error:" + e.getMessage();
}
str = message;
return message;
}
#Override
protected void onPostExecute(String message) {
childrenTextView.setText(str);
}
}.execute(null, null, null);
}
You don't need an API Key in order to register to GCM. You only need a Sender ID (which is the Project ID of the Google API Project you created for your app). The API Key is only needed in the server side for sending GCM messages to your app.
The reason for getting service not available can be many things. You'll have to post your manifest and client registration code to get more help.
//Subtitute with your own project ID
String SENDER_ID = "46086692976";
See screenshot:
I am not very experienced with Android development and I shall include gcm to an existing app.
So far, I can register all devices and I receive the registrationId. The problem is, that I never enter the onMessage() method on my Android 4.0 device. I also tried it with a Galaxy Note 2 (Android 4.1) and there I can receive the message.
I don't get any error message, I just never enter onMessage().
When I send the message, I always get an messageId, so gcm "accepted" the message. But it is never delivered to the device.
How can I find out what the problem is??? Thanks in advance
GCMIntentService.java
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super("82........");
}
#Override
protected void onError(Context arg0, String error) {
System.out.println("Not able to register or unregister because: "
+ error);
}
#Override
protected void onMessage(Context context, Intent intent) {
String message = intent.getStringExtra("message");
System.out.println("GCM DELIVERED MESSAGE TO DEVICE:" + message);
}
#Override
protected void onRegistered(Context arg0, String regId) {
System.out.println("Device is registered with RegisterId: " + regId);
}
#Override
protected void onUnregistered(Context arg0, String regId) {
System.out.println("Device with RegisterId: " + regId
+ " has unregistered");
}
#Override
protected boolean onRecoverableError(Context context, String errorId) {
System.out.println("Entered onRecoverableError");
return super.onRecoverableError(context, errorId);
}
}
My Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activities"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<permission
android:name="com.example.activities.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.activities.permission.C2D_MESSAGE" />
<!-- 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" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Activity which is called first to decide which Activity comes next -->
<activity
android:name=".StartUp"
android:label="#string/title_activity_start_up" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="#string/title_activity_login" >
</activity>
<activity
android:name=".DisplayContent"
android:label="#string/title_activity_display_content" >
</activity>
<activity
android:name=".AllSigns"
android:label="#string/title_activity_all_signs" >
</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.activities" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
Code in onCreate() of my first activity (Startup.java)
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
System.out.println("Id ist leer, Device wird neu registriert");
GCMRegistrar.register(this, "825134311331");
} else {
System.out.println("This device is already registered");
}
If you are trying this over WIFI, make sure that port 5228 isn't being blocked by the network. That's how I fixed the problem that I had after more days of trying to get it to work than I'd like to admit.
Make sure the GCMIntentService class is in the package specified in your manifest