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);
}
Related
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 have a service in my Android app that works perfectly if i started manually, but i need that service to start on boot, and i can't get it started automatically after rebooting the phone, i tryed several tutorials but nothing works.
Here is my manifest code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tvshowsguide"
android:versionCode="1"
android:versionName="1.0"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
<service android:name="androidservice.SyncService" />
<receiver android:name="androidservice.SyncAuto" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is the BroadcastReceiver code:
public class SyncAuto extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent SyncService = new Intent(context, SyncService.class).setAction(SRVC);
context.startService(SyncService);
}
}
}
I think the problem must be the reference of your service, since you have an application with
package="com.tvshowsguide"
Your service and receiver must have a name like:
<service android:name="com.tvshowsguide.androidservice.SyncService" />
<receiver android:name="com.tvshowsguide.androidservice.SyncAuto" >
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>
I have some problem with BroadcastReceiver. There nothing happens when I catch an outgoing call.
public class demoBroadcastReceiver extends BroadcastReceiver {
/** Called when the activity is first created. */
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "there is new calling", Toast.LENGTH_LONG).show();
}
}
This is content of my Manifest:
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".demoBroadcastReceiver">
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
Updated:
Thanks Lucifer for solution:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
I also need this:
<uses-permission android:name="android.permission.NEW_OUTGOING_CALL" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />**strong text**
You should declare this permission in your AndroidManifest.xml file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
maybe you should post your log here, so we can analyse it.
try add this permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Hope this
put this in your manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and this in your application node..
<receiver android:name="com.example.demoBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" /> <--- add this intent filter
</intent-filter>
</receiver>
this is to start a service when a device boots up on android