Reputation: 336
I am writing an android app (minSDK="14") with GCM push notification. App works fine on android higher then 4.0.3(receiving push notification), but on 4.0.3 app don't receive any notification at all.
Already checked:
GcmBroadcastReceiver onReceive method not fired on Android 4.0.3 and
GcmBroadcastReceiver not fired on Android 4.0.3
All permission and package are correct,server part is ok(receive success from GCM ), yet not getting notification, also android support library in my project is 'android-support-v13.jar'
Upvotes: 1
Views: 1127
Reputation: 4477
Use the latest mechanism offered by Google that provides GcmListenerService class, extend this class and override the onMessageRecieved(String from,Bundle data) method.
Upvotes: 0
Reputation: 251
For my apps, I use GCMIntentService
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService()
{
super(senderId);
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
@SuppressLint("NewApi")
private static void generateNotification(Context context, String message)
{
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
PendingIntent intent;// = YourPendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
Notification.Builder builder = new Notification.Builder(context);
builder.setSmallIcon(R.drawable.icon);
builder.setTicker(message);
builder.setWhen(System.currentTimeMillis());
builder.setContentTitle(context.getString(R.string.app_name));
builder.setContentText(message);
builder.setContentIntent(intent);
notification = new Notification.BigTextStyle(builder).bigText(message).build();
}
else
{
notification = new Notification(R.drawable.icon, message, System.currentTimeMillis());
String title = context.getString(R.string.app_name);
notification.setLatestEventInfo(context, title, message, intent);
}
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
@Override
protected void onMessage(Context arg0, Intent arg1)
{
Log.d("GCM", "MESSAGE RECEIVED");
Bundle b = arg1.getExtras();
generateNotification( arg0, b.getString("body"));
}
}
In your manifest add to your application these lines:
<receiver
android:name="my.package.name.GCMReceiver"
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.package.name" />
</intent-filter>
</receiver>
<service
android:name=".GCMIntentService"
android:enabled="true" />
And to finish
public class GCMReceiver extends GCMBroadcastReceiver
{
@Override
protected String getGCMIntentServiceClassName(Context context) {
Log.i("Receiver", "renaming GCM service");
return "my.package.name.GCMIntentService";
}
}
Hope it helps.
Upvotes: 1