Note that there are some explanatory texts on larger screens.

plurals
  1. POdon't receive notifications on android 2.2 using GoogleCloudMessaging
    primarykey
    data
    text
    <p>I'm using the new GoogleCloudMessaging API (from Play Services library, because old Google cloud messaging for android was deprecated) in my project and I have some bugs. When I send messages from my server to all registered devices, my nexus 4 (android 4.4) receives the notification messages, but my Samsung Galaxy Ace doesn't receive them, That's strange, because there are no exceptions or crashes of my project.</p> <p>Any ideas? Thanks.</p> <p>Manifest.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest package="ru.test" android:installLocation="preferExternal" android:versionCode="1" android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;uses-sdk android:minSdkVersion="7" android:targetSdkVersion="17" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt; &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt; &lt;uses-permission android:name="android.permission.VIBRATE"/&gt; &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt; &lt;permission android:name="com.example.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" /&gt; &lt;uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" /&gt; &lt;uses-permission android:name="android.permission.INTERNET"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt; &lt;uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/MyTheme" &gt; &lt;service android:name=".GcmIntentService" /&gt; &lt;receiver android:name=".GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" &gt; &lt;intent-filter&gt; &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt; &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt; &lt;category android:name="com.example.gcm" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;activity android:name="ru.test.MainActivity" android:label="@string/app_name" android:configChanges="orientation|screenSize"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>GcmBroadcastReceiver.java</p> <pre><code>public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String regId = intent.getExtras().getString("registration_id"); if(regId != null &amp;&amp; !regId.equals("")) { /* Do what ever you want with the regId eg. send it to your server */ Log.d(MainActivity.tag, "onReceive " + regId); } // 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); } </code></pre> <p>}</p> <p>GcmIntentService.java</p> <pre><code>public class GcmIntentService extends IntentService { public static int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; NotificationCompat.Builder builder; static final String TAG = "myLogs"; String title = ""; String message = ""; String subtext = ""; public GcmIntentService() { super("GcmIntentService"); } @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); // The getMessageType() intent parameter must be the intent you received // in your BroadcastReceiver. String messageType = gcm.getMessageType(intent); if (!extras.isEmpty()) { // has effect of unparcelling Bundle title = extras.getString("title"); message = extras.getString("message"); subtext = extras.getString("subtext"); /* * Filter messages based on message type. Since it is likely that GCM * will be extended in the future with new message types, just ignore * any message types you're not interested in, or that you don't * recognize. */ if (GoogleCloudMessaging. MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { message = "Send error"; sendNotification(title, message, subtext); } else if (GoogleCloudMessaging. MESSAGE_TYPE_DELETED.equals(messageType)) { message = "Deleted messages on server"; sendNotification(title, message, subtext); // If it's a regular GCM message, do some work. } else if (GoogleCloudMessaging. MESSAGE_TYPE_MESSAGE.equals(messageType)) { // Post notification of received message. sendNotification(title, message, subtext); Log.d(TAG, "Received: " + extras.toString()); } } // Release the wake lock provided by the WakefulBroadcastReceiver. GcmBroadcastReceiver.completeWakefulIntent(intent); } // Put the message into a notification and post it. // This is just one simple example of what you might choose to do with // a GCM message. private void sendNotification(String title, String message, String subtext) { mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SplashScreen.class), 0); NotificationCompat.BigTextStyle bigxtstyle = new NotificationCompat.BigTextStyle(); bigxtstyle.bigText(message); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_small) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_large)) .setTicker(message) .setContentTitle(title) .setDefaults(Notification.DEFAULT_ALL | Notification.FLAG_AUTO_CANCEL) //.setContentInfo("Content info") .setAutoCancel(true) .setSubText(subtext) .setWhen(System.currentTimeMillis()) .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) .setContentText(message); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(++NOTIFICATION_ID, mBuilder.build()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload