I got a program including GCM service and the GCM service performs perfectly on some of my devices
including:
1)android emulator (Google API 17 - 4.2.2)
2)LG Phone (Android 4.1.2)
3)Google phone (Android 4.2.2)
However, on the following device it seems the GCM function stops at registration status and
onRegistered() was not called.
including:
1)Sony (Android 4.0.4)
2)HTC (Android 4.0.3)
3)SAMSUNG Tablet (Android 3.0.3)
Since it can run on some of my devices, I am pretty sure the problem is not in my code or the Android manifest permission settings, but I don't know what can I add or modify to solve this.
I knew that GCM function for version 4.0.4 and lower needs an active Google account, but I tried it and it's still not working.
Meanwhile I tried the solution suggested in these two questions, but it didn't help
I can not get registration ID from Android GCM
Android GCM : GCMRegistrar gives empty registration ID
Can anyone give me some advice how to solve this problem?
Any other way I can perform server push notification function?
Here are my code
//GCM part
cd = new ConnectionDetector(getApplicationContext());
// Check if Internet present
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(this,
"Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Getting name, email from intent
Intent i = getIntent();
// Make sure the device has the proper dependencies.
try{
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Version too old" + e.toString(), Toast.LENGTH_LONG).show();
}
registerReceiver(mHandleMessageReceiver, new IntentFilter(
DISPLAY_MESSAGE_ACTION));
// Get GCM registration id
final String regId = GCMRegistrar.getRegistrationId(this);
// Check if regid already presents
if (regId.equals("")) {
// Registration is not present, register now with GCM
try{
GCMRegistrar.register(this, SENDER_ID);
Toast.makeText(getApplicationContext(), "Trying to register", Toast.LENGTH_LONG).show();
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "Registration failure", Toast.LENGTH_LONG).show();
}
} else {
// Device is already registered on GCM
if (GCMRegistrar.isRegisteredOnServer(this)) {
// Skips registration.
Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
} else {
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
// Register on our server
// On server creates a new user
ServerUtilities.register(context, userId, "123", regId);
return null;
}
#Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
And also Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.activity"
android:versionCode="1"
android:versionName="2" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- GCM connects to Internet 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" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.test.activity.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.test.activity.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<application
.
.
.
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.test.activity" />
</intent-filter>
</receiver>
<service android:name="com.test.activity.GCMIntentService" />
</application>
</manifest>
I'm not sure if it's related to your problem, but this :
<uses-permission android:name="com.test.activity.C2D_MESSAGE" />
Should be changed to this :
<uses-permission android:name="com.test.activity.permission.C2D_MESSAGE" />
Related
I'm trying to implement push notification in an Android app.
As I understand, we need a sender and receiver installed on the device.
I googled on GCM an found that I need to create a Project in Google dev console.
I got a project ID and an API key.
I found there are third party applications (mixpanel) which can send
push notifications with the information above. Can I use Google Play to
send notifications to all app users?
On the programming side, I got permissions required in the Manifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM connects to Internet 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" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.androidexample.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.androidexample.gcm.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
And a broadcast receiver
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.androidexample.gcm" />
</intent-filter>
</receiver>
<service android:name="com.androidexample.gcm.GCMIntentService" />
From here on, I got confused about the "sender" and "receiver" parts.
Do I have to implement GCMBroadcastReceiver?
If I want to implement only the Receiving notification part, how does it look like ?
Any reference to a working code example will be good.
Yes, you have to implement GcmBroadcastReceiver class to catch messages from GCM service. The purpose of the receiver is to start the intent service even if the app is off. Here is the most common implementation:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
The purpose of the intent service is doing something with the caught GCM message:
public class GCMIntentService extends IntentService {
#Override
protected void onHandleIntent(Intent intent) {
// Getting GCM message fields
Bundle extras = intent.getExtras();
// Doing something with the message.
// E.g. showing push notification
}
}
You can see e.g. this article for details.
I'm trying to implement Push Notification for my application. First of all, I've used old/depracated DemoAcitivity sample and I succeeded to receive messages from server to my app.
Since it's deprecated, I gave it a try to new GCM API in order to implement the identical Push Notif. service. During this implementation, I mostly used the code provided here;
http://developer.android.com/google/gcm/gs.html
In my code, each time I try to register my device to GCM, I get SERVICE_NOT AVAILABLE error. The method I use to register is the exactly the smae in the docs(linked above);
private void registerBackground() {
new AsyncTask<Void, Object, String>() {
#Override
protected String doInBackground(Void... objects) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(GenericUtilities.SENDER_ID);
msg = "Device registered, registration id=" + regid;
// You should send the registration ID to your server over HTTP,
// so it can use GCM/HTTP or CCS to send messages to your app.
// For this demo: we don't need to send it because the device
// will send upstream messages to a server that echo back the message
// using the 'from' address in the message.
// Save the regid - no need to register again.
setRegistrationId(context, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
#Override
protected void onPostExecute(String msg) {
mDisplay.append((String)msg.toString() + "\n");
}
}.execute(null, null, null);
}
And this is the manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pushnotificationexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<!-- 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" />
<!--
Creates a custom permission so only this app can receive its messages.
NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
where PACKAGE is the application's package name.
-->
<permission
android:name="com.example.pushnotificationexample.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission
android:name="com.example.pushnotificationexample.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.pushnotificationexample.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.example.pushnotificationexample.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.pushnotificationexample" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>
Any ideas?
I'm not sure whether it would fix your problem, but the following line is no longer necessary and should be removed from your manifest :
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
In addition, you no longer need the following line (though it shouldn't have anything to do with the error you get):
<service android:name=".GCMIntentService" />
8/23/13 EDIT:
When I built my app with API 8 and declared in my manifest:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
I didn't get this error.
After I built my app with API 10 and changed the manifest to:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10"/>
I got this error. I don't know if it helps, since your targetSdkVersion is 17, so you may be using advanced features.
I'm having a problem with GCM on Android. It fails to execute the onRegistered() callback, or ever return a good regId, on one of my test devices (Droid2) - but it works perfectly well on another device (Galaxy Nexus).
I'm following the basic example here. The caller looks like this:
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals(""))
GCMRegistrar.register(this, Constants.SENDER_ID);
else
Log.v(TAG, "Already registered");
And my manifest has this:
<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" />
and
<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.gcl.myapp" />
</intent-filter>
</receiver>
Why isn't the onRegistered() callback working? And what could be causing it to work on one device, but fail on another?? Thanks.
If it works on some devices but not others then it is to do with one of the following:
Pre Android 4.0.4 the device requires a valid google account to work.
GCM only works on devices with Play Store App and API 8 onwards.
Your manifest is badly formatted - use Lint to check!
Device/App is already registered, the Play implementation may not return again.. Try GCMRegistrar.unregister(this); first.
Hope that helps!
I had the same problem. If you are using AngularJS + IonicFramework you do not have to do this:
After you create your factory with your onDeviceReady function, creates onNotificationGCM function. Something like this:
app.factory('PushProcessingService', function () {
..
});
function onNotificationGCM(e) {
}
I was creating onNotificationGCM inside my factory. This solve my problem.
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.
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.
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
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... =\
I have tried some forum pages and searched through stackoverflow for the same thing, but I did not find the solution.
I have used the below code to generate registration ID for GCM notification. But I get an empty string as a registration ID:
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
if (GCMRegistrar.isRegistered(this)) {
Log.d("info", GCMRegistrar.getRegistrationId(this));
}
regId = GCMRegistrar.getRegistrationId(this);
Log.v("log", " register id oslo : " + regId);
if (regId.equals("")) {
// replace this with the project ID
GCMRegistrar.register(this, "718437041388");
Log.d("info", GCMRegistrar.getRegistrationId(this));
} else {
Log.d("info", "already registered as" + regId);
}
Please help me with this, tell me if I got anything wrong.
Thank you in advance,
Nirav.
You should not get the registrationId just after GCMRegistrar.register(this, "718437041388");
In fact, do you have a correct GCMIntentService.java which receive the regId?
If yes, you can get the regId from the onRegistered inside GCMIntentService.java
#Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
Log.d("GCMIntentService", "in GCMIntentService");
displayMessage(context, getString(R.string.gcm_registered));
ServerUtilities.register(context, registrationId);
}
Notice that
GCMRegistrar.register(this, "...");
is asynchronous. Therefore, calling GCMRegistrar.getRegistrationId(this) immediately after that is not reliable, so you should avoid it.
The id will arrive via broadcast callback to your GCMIntentService, as a part of the registration procedure. from there you can than store the gcm key anywhere you like, and use it in other parts of the application (usually on the server).
Notice that GCMIntentService should be defined at the root package of your app for it to work.
Try using emulator with Google API and also sync with a Google account.
I have same problem after 1 day struggle I find solution.
First i Put my GCM Related Code in To My Main Package and make change according to My Package in androidManifest.xml
i give you simple example. My project name is GCMExample and i have Three package
1. com.examle.GCMExample(Main Package) ,
2. com.examle.GCMExample.activity(Second Package) ,
3. com.example.GCMExample.adapter(Third Package)
I am not Getting Registration Id When My GCM Related Class File Into My Second Package
My GCM Related class like 1. GCMIntentService , 2. ServerUtilities , 3. CommonUtilities 4. WakeLocker put Into My Main Package com.example.GCMExample
also my androidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<permission android:name="com.example.GCMExample.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.GCMExample.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.GCMExample" />
</intent-filter>
</receiver>
<service android:name="com.example.GCMExample.GCMIntentService" />
Add GCM jar file in your project's lib folder