Maybe a second set of eyes can help me here. Can't seem to get anything in my Receiver, even though I'm getting successful sends when posting to gcm send url from my server.
Manifest:
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="<pkgname>.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="<pkgname>.permission.C2D_MESSAGE" />
<receiver
android:name="<pkgname>.GcmBroadcastReceiver"
android:exported="true"
android:permission="com.google.android.gcm.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="<pkgname>" />
</intent-filter>
</receiver>
<service android:name="<pkgname>.GcmIntentService" />
= com.myappsname I've removed it for privacy sake
Here is the server request and response:
{"registration_ids":[removed],"data":{"project":{"comment":"Chubb","username":"Dave Thomas","time":"2014-12-05 15:33:13","projectId":5}}}
{"multicast_id":removed,"success":2,"failure":0,"canonical_ids":1,"results":[{"message_id":"removed"},{"registration_id":"removed","message_id":"removed"}]}
So server is sending successfully... but not seeing anything on the app side at all. Put a break point at the start of the receiver's onReceive method, and it isn't catching anything.
receiver:
package <pkgname>;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#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);
}
}
Update found this in my log, so I know device is actually geting message:
12-08 12:48:42.910: W/BroadcastQueue(960): Permission Denial: broadcasting Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10 pkg=<pkgname> (has extras) } from com.google.android.gsf (pid=28999, uid=10007) requires com.google.android.gcm.c2dm.permission.SEND due to receiver <pkgname>/.GcmBroadcastReceiver
I think I've found my issue
I see you are missing two permissions
<uses-permission android:name="<pkgname>.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and into your receiver declaration android:authorities="<pkgname>"
<receiver
android:name="<pkgname>.GcmBroadcastReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND"
android:authorities="<pkgname>">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="<pkgname>" />
</intent-filter>
</receiver>
The problem is in my receiver decleration in my AndroidManifest:
<uses-permission android:name="com.google.android.gcm.c2dm.permission.SEND"/>
Should be:
<uses-permission android:name="com.google.android.c2dm.permission.SEND"/>
Related
I am trying to receive ACTION_DOWNLOAD_COMPLETE
Please note that download is initiated by chrome and then taken over by DownloadManger.
here is my relevant manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application>
<receiver
android:name=".receivers.DownloadBroadcastReceiver"
android:directBootAware="true"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
</application>
Here is the DownloadBroadcastReceiver
public class DownloadBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("TAG", "onReceive: A download happened");
}
}
EDIT : I tried adb to send intent
adb -d shell am broadcast -a android.intent.action.DOWNLOAD_COMPLETE
and then my app responds, looks like DownloadMangeris not sending the intent.
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
I have researching for hour and hour and found many similar question but have no a working solution for my question.
I'm understand if I close the app by force close that is normal that I not receive data from gcm. But I was unable to receive the notification with gcm until I start the app. So the application is running either foreground or background then I only be able to receive the data from gcm but when I close the app by sliding away it (which is not done by force close) the notification didn't show up which mean gcm wasn't receive any data at all. . So it mean I need to stay active but if I do so then there is no point for using gcm anymore which design for pushing notification.
Here is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.khooteckwei.google" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<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.gcmtesting.google.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcmtesting.google.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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.example.gcmtesting.google" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
</application>
</manifest>
here is the wakefulBroadcastReceiver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#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);
}
}
The notification working fine when the app is active so I didn't include the function that use to display notification.Is there any problem with this 2 pages?
I think your problem is in your GcmBroadcastReceiver receiver, you can look at this link , I think it's will help you with GCM http://www.intertech.com/Blog/push-notifications-tutorial-for-android-using-google-cloud-messaging-gcm/
I think notifications doesn't work because your receiver doesn't check do your app running or not , if it is you need to handle notification or if not show android notification
I'm trying to add push notification to my android app.
Everything seems ok :
Sender id is ok
device is correctly registered to my database
notification is correctly sent to GCM server (success:1)
Broadcast receiver correctly receives the message (i see it trough the Eclipse console)
... but the notification is never displayed, although :
the app is running, or in background
the "Show/Display notifications" is checked for this app in the general settings of the device
the permissions seem ok to me...
Here is my code. Any clue about what i did wrong ? Thanks !
Manifest (partial)
<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.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="<MY-APP>.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="<MY-APP>.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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="<MY-APP>" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
Broadcast Receiver
package <MY-APP>;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class GcmBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("gcm_debug", "PushReceiver onReceive called");
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String msgType = gcm.getMessageType(intent);
if(!extras.isEmpty()){
if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(msgType)){
Log.i("gcm_debug", "Message send error");
}else if(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(msgType)){
Log.i("gcm_debug", "Message deleted");
}else if(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(msgType)){
Log.i("gcm_debug", "Message received : " + extras.toString());
}
}
setResultCode(Activity.RESULT_OK);
}
}
Gcm push the message to the registered device, and device receive as a message not as Notification.
Please check , your code if you are receiving the message in your device, if yes use the NotificationManager to show as notification.
for more details please check this.
On your Google Play console you need to make sure that the server IP that´s going to send the notification it´s Ok.
You can have a 0.0.0.0/0 ip for your local dev server and a real IP for the production server.
Just like this.
In my regular build gcm works fine, but when I use flavors to change the com name things stop working. My IntentService is never being initialized. How should I set up my manifest files so that this happens?
I have added them to the developer console.
I am using gradle com.android.tools.build:gradle:0.11.+'
I get the logs
V/GCMBroadcastReceiver﹕ onReceive: com.google.android.c2dm.intent.REGISTRATION
V/GCMBroadcastReceiver﹕ GCM IntentService class: com.sample.app.dev.GCMIntentService
V/GCMBaseIntentService﹕ Acquiring wakelock
and then nothing. My custom IntentService class never gets initialized and my registrationId stays empty. My current implementation is similar to the one here: https://stackoverflow.com/a/20334397/198034
How should I set up my manifest to get gradle 0.11.+ flavors to work with gcm?
Below is a manifest in one of my flavors, I have more code in the main manifest (all needed permissions, etc), but nothing else dealing with gcm.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.app"
android:versionCode="45"
android:versionName="0.6.5" >
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.sample.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.sample.app.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name" >
<service android:name=".GCMIntentService" />
<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="${packageName}" />
</intent-filter>
</receiver>
</application>
</manifest>
This was my solution:
<permission android:name="com.sample.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.sample.app.permission.C2D_MESSAGE" />
...
<service android:name=".GCMIntentService" />
<receiver
android:name=".MyBroadcastReceiver"
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.sample.app" />
</intent-filter>
</receiver>
in my manifest and mybroadcastReceiver
...
public class MyBroadcastReceiver extends GCMBroadcastReceiver
{
#Override
protected String getGCMIntentServiceClassName(Context context)
{
return GCMIntentService.class.getName();
}
}
From your manifest it looks like your app's package name is com.sample.app, which is consistent with your permission.C2D_MESSAGE definition. Your receiver's intent filter's category says ${packageName}. Hopefully that gets translated to com.sample.app too, as it should.
Other than that, your log posted above shows that your intent service is expected to be at com.sample.app.dev.GCMIntentService. That's a different package.
You seem to be using the old deprecated GCM client library, in which com.google.android.gcm.GCMBroadcastReceiver expects the intent service to be called GCMIntentService and be located in the main package of the app :
/**
* Gets the default class name of the intent service that will handle GCM
* messages.
*/
static final String getDefaultIntentServiceClassName(Context context) {
String className = context.getPackageName() +
DEFAULT_INTENT_SERVICE_CLASS_NAME;
return className;
}
Your manifest declares the intent service class to be in the main package of your app :
<service android:name=".GCMIntentService" />
All these things don't add up. Either your app's package name is com.sample.app.dev or com.sample.app. If it's the former, the manifest you posted is incorrect. If it's the latter, there is no reason for the broadcast receiver to expect the intent service class to be com.sample.app.dev.GCMIntentService.