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
Related
Actually my application works fine and able to receive GCM push notifications but there are some of devices which are not receiving GCM notification in GCMIntentService only when my app is in background. devices are huwai honor,xiaomi's some models. So bellow is my code, Let me know if anybody gone through this problem earlier. Can anyone please help me on this?
Android manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.testapp"
android:installLocation="preferExternal"
android:versionCode="41"
android:versionName="4.1.7" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<permission
android:name="com.app.testapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.app.testapp.permission.C2D_MESSAGE" />
<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" />
<uses-permission android:name="android.permission.READ_LOGS" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<application
android:name="com.app.testapp.MyApplication"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/SampleTheme">
<activity
android:name="com.app.testapp.activity.SplashActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/ActivityTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- GCM -->
<receiver
android:name="com.app.testapp.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.app.testapp" />
</intent-filter>
</receiver>
<!-- Services -->
<service android:name="com.app.testapp.GCMIntentService" />
receiver:
public class GCMBroadcastReceiver extends
com.google.android.gcm.GCMBroadcastReceiver {
#Override
protected String getGCMIntentServiceClassName(Context context) {
return "com.app.testapp.GCMIntentService";
}
}
Service:
package com.app.testapp;
import android.content.Context;
import android.content.Intent;
import com.app.testapp.util.Constants;
import com.app.testapp.util.Logger;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
private static String TAG = "GCM";
public MyGCMIntentService() {
super(Constants.SENDER_ID);
}
#Override
protected void onError(Context arg0, String arg1) {
Logger.logger(TAG, "onError");
}
String message = null;
#Override
protected void onMessage(Context context, Intent intent) {
message = intent.getExtras().getString("message");
Logger.logger(TAG, "onMessage message=" + message);
}
#Override
protected void onRegistered(Context arg0, String arg1) {
Logger.logger(TAG, "onRegistered");
}
#Override
protected void onUnregistered(Context arg0, String arg1) {
Logger.logger(TAG, "onUnregistered");
}
}
I was updating my app to new GCM 7.5 while I noticed thatgoogle changed lot of things.
take a look at my code
public class GcmIntentService extends GcmListenerService {
public static final int NOTIFICATION_ID = 1;
private static final String TAG = "MyGcmListenerService";
public GcmIntentService() {
super();
}
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
String message2 = data.getString("payload");
String title = data.getString("title");
String type = data.getString("push_type");
Log.i(TAG, "Message: " + message2 + " PushType:" + type + "title" + title);
for (String key : data.keySet())
{
Log.d("Bundle Debug", key + " = \"" + data.get(key) + "\"");
}
// parseMessage(data);
// sendNotification(message);
}
#Override
public void onDeletedMessages() {
// sendNotification("Deleted messages on server");
}
#Override
public void onMessageSent(String msgId) {
// sendNotification("Upstream message sent. Id=" + msgId);
}
#Override
public void onSendError(String msgId, String error) {
// sendNotification("Upstream message send error. Id=" + msgId + ", error" + error);
}
Now into my problem: The data recieved is null except I logged the bundle there was a collapsekey too.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.was.abcd"
android:versionCode="404080000"
android:versionName="4.8.0" >
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="false"
android:xlargeScreens="true" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<!-- * Google Cloud Messaging -->
<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.was.abcd.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.was.abcd.permission.C2D_MESSAGE" />
<!-- Google Cloud Messaging * -->
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:allowBackup="false"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:largeHeap="#bool/largeheap"
android:theme="#style/Theme.FullScreen" >
<!-- * Google Cloud Messaging -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<!-- Google Cloud Messaging * -->
<activity
android:name="com.magazine.screens.ShelfActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.magazine.screens.SplashActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.magazine.screens.ReaderActivity"
android:configChanges="keyboardHidden|screenSize"
android:windowSoftInputMode="adjustPan" >
</activity>
<activity
android:name="com.magazine.screens.PurchaseActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.magazine.screens.FacebookActivity"
android:configChanges="orientation"
android:hardwareAccelerated="true"
android:icon="#drawable/fb_logo"
android:screenOrientation="portrait" >
</activity>
<!-- * Google Cloud Messaging -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.was.abcd" />
</intent-filter>
</receiver>
<activity
android:name="com.magazine.messaging.NotificationDialogActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" >
</activity>
<service
android:name="com.magazine.messaging.GcmIntentService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/app_id" />
<activity
android:name="com.facebook.LoginActivity"
android:label="#string/app_name" >
</activity>
</application>
Please take a look at these points:
As per Google Documentation make use of GCMReceiver instead of WakefulBroadcastReceiver.
Also if you are using gcm.register() please make us of InstanceId as the former has been deprecated.
Hope that helps!!
In the log I am getting this message.
V/GCMBroadcastReceiver(30668): onReceive: com.google.android.c2dm.intent.REGISTRATION
08-07 11:12:59.096: V/GCMBroadcastReceiver(30668): GCM IntentService class: com.example.notifications.GCMIntentService
08-07 11:12:59.096: V/GCMBaseIntentService(30668): Acquiring wakelock
But OnRegistered method is not been invoked ( Running app in my MotoG device ). Below is the manifest.xml coding.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notifications"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<permission android:name="com.example.notifications.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.notifications.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
android:name=".MainActivity"
android:label="title_activity_main" >
<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.notifications" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>
And my MainActivitiy.java file
public static final String SENDER_ID = "xxxxxxxxx";
GCMRegistrar.checkDevice(MainActivity.this);
GCMRegistrar.checkManifest(MainActivity.this);
ChannelUri = GCMRegistrar.getRegistrationId(getApplicationContext());
if(ChannelUri.equals(""))
{
GCMRegistrar.register(getApplicationContext(), SENDER_ID);
}
else
{
Toast.makeText(getApplicationContext(), "Already Registered", Toast.LENGTH_SHORT).show();
}
GCMIntentService.java
public class GCMIntentService extends GCMBaseIntentService {
public static String sRegistrationId;
protected GCMIntentService() {
super(MainActivity.SENDER_ID);
}
public static String getRegistrationId()
{
return sRegistrationId;
}
#Override
protected void onError(Context arg0, String arg1) {
Log.e("Registration", "Got an error!");
Log.e("Registration", arg0.toString() + arg1.toString());
}
#Override
protected void onMessage(Context arg0, Intent intent) {
Log.i("Registration", "Got a message!");
Log.i("Registration", arg0.toString() + " " + intent.toString());
}
#Override
protected void onRegistered(Context arg0, String arg1) {
sRegistrationId = arg1;
Log.i("Registration", "Just registered!");
Log.i("Registration", arg0.toString() + arg1.toString());
}
#Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
}
Project Structure
Src -> com.example.notifications -> GCMIntentService.java, MainActivity.java
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 had this message when running the project:
Unfortunately the project stopped
also when I add the code between try and catch I had this error
application dosenot define permission com.example.elarabygroup.permission.c2d MESSAGE
I tested all codes and I found the error to be here:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_elaraby_group);
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");
}
}
manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.elarabygroup"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 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
android:name=".ElarabyGroup"
android:label="#string/title_activity_elaraby_group" >
<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=".ElarabyGroup" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>
GCMIntentService class
package com.example.elarabygroup;
import com.google.android.gcm.GCMBaseIntentService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class GCMIntenetService extends GCMBaseIntentService {
public static String TAG = "GCMIntentService";
public GCMIntenetService(String senderId) {
super(senderId);
Log.d("GCMIntentService", senderId);
}
#Override
protected void onError(Context arg0, String arg1) {
Log.d("onError", arg1);
}
#Override
protected boolean onRecoverableError(Context context, String errorId) {
Log.d("onRecoverableError", errorId);
return false;
}
#Override
protected void onMessage(Context arg0, Intent arg1) {
Log.d("onMessage", String.valueOf(arg1));
}
#Override
protected void onRegistered(Context arg0, String arg1) {
Log.d("onRegistered", arg1);
}
#Override
protected void onUnregistered(Context arg0, String arg1) {
Log.d("onUnregistered", arg1);
}
}
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Try this