new Google Cloud Messaging API - android

I'm sending a push notification using PHP using curl, and the result seems to return fine:
{"multicast_id":2345735671345346,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:13456457587969%375ed23445237ecd"},{"message_id":"0:12344526107806%375ed3439fd7ecd"}]}
So I'm guessing the message is sent, and the problem is on the BroadcastReceiver subclass.
My Broadcast receiver code:
package com.example.myexample.pushnotifications;
public class GCMBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
Log.d(TAG, "onReceive method executed properly");
//NotificationManager and NotificationCompat.Builder code to build a notification
}
}
then my AndroidManifest.xml for the permissions:
<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" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.example.myexample.pushnotifications.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.myexample.pushnotifications.permission.C2D_MESSAGE" />
and for the receiver inside the application tag:
<receiver
android:name="com.example.myexample.pushnotifications.GCMBroadcastReceiver"
android:enabled="true"
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.example.myexample.pushnotifications" />
</intent-filter>
</receiver>
The problem is that the broadcast receiver never receives the message.

Assuming the package of the app is com.example.myexample, the following changes are required :
<permission
android:name="com.example.myexample.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.myexample.permission.C2D_MESSAGE" />
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.myexample" />
</intent-filter>

Related

Android receive GCM while display is off

I am using GCM to send some data from my server to my app.
The messaging is working fine. I receive every massage instantly on my device.
However, The problem is: It does not work when the display of my smartphone is off.
Maybe it has s.th. to do with my smartphone? I'm running Android 5 and notification from other apps work fine (like whatsapp).
server data:
{
"data": {
"id": "5",
"message": "test"
},
"registration_ids": ["myRegId"],
"priority": "high"
}
Manifest:
...
<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.example.gcm" />
</intent-filter>
</receiver>
<service
android:name=".GcmMessageHandler"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</application>
<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="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.asdf.myapp.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.asdf.myapp.gcm.permission.C2D_MESSAGE" />
Receiver:
public class GcmMessageHandler extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
Log.d("test", "message received");
}
}
Your receiver class should extend from WakefulBroadcastReceiver to guaranteeing that the CPU is awake so that your listener service can complete its task even if your device is lock.
Manifest
<permission
android:name="com.oostaa.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.oostaa.app.permission.C2D_MESSAGE" />
<receiver
android:name=".receiver.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.oostaa.app" />
</intent-filter>
</receiver>
Note: com.oostaa.app is my package name.
Receiver:
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);
}
}
I noticed, that the issue only occurs if I'm connected to my WLAN.
When WLAN is disabled, I get the GCM message no matter what.
I've read this reddit post and followed its instructions which seemed to solve my issues.
Reddit

Not receiving GCM messages on Android after app is swiped away from recent tasks

I am implementing GCM for push notifications using GcmReceiver and GcmListenerService. When the app is running in the foreground or background, I receive the notifications just fine.
My problem is when I swipe the app away from the "recent tasks" list, I can't receive GCM messages anymore (I am on a Xiaomi Mi3). I understand that if you force close an app, you will not receive push notifications anymore. However, swiping it away from the "recent tasks" should still allow the app to receive push notifications.
I have tested this with WeChat and Facebook Messenger, and I do still receive notifications when I swipe the app away (but not if I force close it from Settings).
What am I doing wrong here?
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.considerdigital.p1mobileapp" >
<permission android:name="com.considerdigital.p1mobileapp.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.considerdigital.p1mobileapp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.google.android.c2dm.permission.SEND" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="#drawable/p1_app_icon"
android:label="#string/app_name"
android:theme="#style/Theme.P1mobileapp" >
<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.considerdigital.p1mobileapp" />
</intent-filter>
</receiver>
<service
android:name=".ListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
GcmListenerService:
public class ListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
public static final String FOREGROUND_NOTIFICATION = "FOREGROUND_NOTIFICATION";
private NotificationManager mNotificationManager;
#Override
public void onMessageReceived(String from, Bundle data) {
// Send notification only if app is in background
if (!P1GlobalVariables.getSharedInstance().isAppActive) {
System.out.println("App is inactive");
sendNotification(data);
} else {
System.out.println("App is active");
sendForegroundNotification(data);
}
Log.i(TAG, "Received: " + data.toString());
}

Android GCM receive not working

I'm trying to set up push notifications in an app for the first time. I think the server side is OK (the message send to google comes back with status code 200, and I see a success result in the response body).
But the device never does anything :(
Manifest is set as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="my.package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="my.package.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/appName"
android:theme="#style/AppTheme" >
<activity
android:name="my.package.activity.MainActivity"
android:label="#string/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.gms.gcm.GcmReceiver"
android:enabled="true"
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="my.package" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
</intent-filter>
</receiver>
<service
android:name=my.package.NotificationListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</application>
</manifest>
And the NotificationListenerService is like this:
public class NotificationListenerService extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d("MyApp", "message);
Notification.ShowNotification("test", getApplicationContext());
}
}
I think that's all I need according to https://developers.google.com/cloud-messaging/android/client (asides from the stuff to handle reset tokens which I've not added yet. My token registration seems to work as I can see the token in the dev console data store.)
I've tried looking at logcat but nothing obvious seems to appear. Do I need to "start" the service in some way? The documentation suggests not... I'm obviously missing something fundamental though!
When you send a message, your server normally gets a message_id as a response. Using the diagnostics tool with the message_id could be useful to make sure that GCM correctly relayed the message.
Is Google Play Services up-to-date on your device ?

Can't receive notifications any more, Android with GCM

I'm trying to use Google Cloud Messaging in one my android applications, everything was going fine but once I changed the package structure of my project I couldn't receive notifications , here is my new Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gcm"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.gcm.actvities.RegisterActivity"
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.gcm.utils.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.gcm" />
</intent-filter>
</receiver>
<service android:name="com.example.gcm.services.GCMNotificationIntentService" />
</application>
You haven't included the code of com.example.gcm.utils.GcmBroadcastReceiver, but since your trouble started after changing packages, it is very likely that the code of the broadcast receiver that starts the intent service uses context.getPackageName() as the package of the intent service, which is incorrect after your change. You should specify the correct package of the intent service class.
Assuming you used the standard code from the GCM demo in your receiver, it probably look like this :
#Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
In that case, change it to this :
#Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(GcmIntentService.class.getPackage().getName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}

Permission Denial: receiving Intent { act=com.google.android.c2dm.intent.REGISTRATION

I'm trying to implement push notifications in my app, but it seems that my C2DMReceiver is not working. I tried every solution I could find. Here is what I have:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10" />
<permission android:name="com.my.app.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="com.my.app.permission.C2D_MESSAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<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.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<activity>....</activity>
<service android:name=".C2DMReceiver"/>
<receiver
android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.my.app"/>
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
<category android:name="com.my.app"/>
</intent-filter>
</receiver>
In my onCreate method I use:
C2DMessaging.register(this, "mymail#gmail.com");
String id = C2DMessaging.getRegistrationId(this);
And id is always empty.
Looking at the LogCat, I can see Permission Denial: receiving Intent { act=com.google.android.c2dm.intent.REGISTRATION So, what should I do?Thanks)
Change your android:name in your receiver tag from com.google.android.c2dm.C2DMBroadcastReceiver to com.my.app.YourReceiverClassName, and make sure your sender e-mail and server side login e-mail are the same.
The android:name in your receiver has to specify the path to your class that implements the receiver. I am sure that your C2DMBroadcastReceiver class is not in the com.google.android.c2dm package, as I see below your are using a com.my.app package.
Create a C2DMBroadcastReceiver class in, for example, com.my.app.receiver and specify the receiver's adroid:name as:
<receiver
android:name="com.my.app.receiver.C2DMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
......
</receiver>

Categories

Resources