Note that there are some explanatory texts on larger screens.

plurals
  1. POPush Notification receiver is not working
    primarykey
    data
    text
    <p>I'm implementing push notification service i my app, I followed the tutorial <a href="http://developer.android.com/google/gcm/gs.html" rel="nofollow">on Android developers.com</a>, but unfortunately, i did not receive any notifications.</p> <p>I'm receiving the registration id successfully, but,the device doesn't receive any notifications from the back end, although the back ed service receives a successful response.</p> <p>p.s : I'm sure from the implementation of the back end service that sends the notification to Google cloud servers.</p> <p>Could any one help me solving this problem..?, thanks in advance.</p> <p>Below you can find my implementation.</p> <h2>The Manifest.xml</h2> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ettamen.app" android:versionCode="1" android:versionName="1.0" &gt; &lt;supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" /&gt; &lt;!-- GCM connects to Internet Services. --&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;!-- GCM requires a Google account. --&gt; &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt; &lt;!-- Keeps the processor from sleeping when a message is received. --&gt; &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt; &lt;!-- Creates a custom permission so only this app can receive its messages. --&gt; &lt;permission android:name="com.ettamen.app.permission.C2D_MESSAGE" android:protectionLevel="signature" /&gt; &lt;uses-permission android:name="com.ettamen.app.permission.C2D_MESSAGE" /&gt; &lt;!-- This app has permission to register and receive data message. --&gt; &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt; &lt;!-- Network State Permissions to detect Internet status --&gt; &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt; &lt;!-- Permission to vibrate --&gt; &lt;uses-permission android:name="android.permission.VIBRATE" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.RECEIVE_SMS" /&gt; &lt;uses-permission android:name="android.permission.READ_CONTACTS" /&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt; &lt;uses-permission android:name="android.permission.BROADCAST_STICKY" /&gt; &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.CALL_PHONE" /&gt; &lt;uses-permission android:name="android.permission.SEND_SMS" /&gt; &lt;application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" &gt; &lt;activity android:name="Ettamen" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:theme="@android:style/Theme.Black.NoTitleBar" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;receiver android:name="com.ettamen.app.pushnotifications.CordovaGCMBroadcastReceiver" android:exported="true" 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;category android:name="com.ettamen.app.pushnotifications" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;service android:name="com.ettamen.app.pushnotifications.GCMIntentService" &gt; &lt;/service&gt; &lt;activity android:name=".pushnotifications.PushHandlerActivity" &gt; &lt;/activity&gt; &lt;service android:name="LocationUpdateService" &gt; &lt;/service&gt; &lt;/application&gt; &lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /&gt; &lt;/manifest&gt; </code></pre> <h2>MyReceiver.java</h2> <pre><code>import com.ettamen.app.R; import com.google.android.gms.gcm.GoogleCloudMessaging; /* * Implementation of GCMBroadcastReceiver that hard-wires the intent service to be * com.plugin.gcm.GCMIntentService, instead of your_package.GCMIntentService */ public class CordovaGCMBroadcastReceiver extends BroadcastReceiver { static final String TAG = "GCMDemo"; public static final int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; NotificationCompat.Builder builder; Context ctx; @Override public void onReceive(Context context, Intent intent) { GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); ctx = context; String messageType = gcm.getMessageType(intent); if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { sendNotification("Send error: " + intent.getExtras().toString()); } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED .equals(messageType)) { sendNotification("Deleted messages on server: " + intent.getExtras().toString()); } else { sendNotification("Received: " + intent.getExtras().toString()); } setResultCode(Activity.RESULT_OK); } // Put the GCM message into a notification and post it. private void sendNotification(String msg) { mNotificationManager = (NotificationManager) ctx .getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, new Intent(ctx, PushHandlerActivity.class), 0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( ctx).setSmallIcon(R.drawable.ic_launcher) .setContentTitle("GCM Notification") .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setContentText(msg); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); } } </code></pre> <ul> <li><p>GCM Intent Service </p> <hr> <pre><code>import android.app.ActivityManager.RunningTaskInfo; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.util.Log; public class GCMIntentService extends GCMBaseIntentService { public static final int NOTIFICATION_ID = 237; private static final String TAG = "GCMIntentService"; public GCMIntentService() { super("GCMIntentService"); } @Override public void onRegistered(Context context, String regId) { } @Override public void onUnregistered(Context context, String regId) { } @Override protected void onMessage(Context context, Intent intent) { Log.d(TAG, "onMessage - context: " + context); // Extract the payload from the message Bundle extras = intent.getExtras(); if (extras != null) { createNotification(context, extras); } } public void createNotification(Context context, Bundle extras) { NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); String appName = getAppName(this); Intent notificationIntent = new Intent(this, PushHandlerActivity.class); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); notificationIntent.putExtra("pushBundle", extras); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( context).setSmallIcon(context.getApplicationInfo().icon) .setWhen(System.currentTimeMillis()).setContentTitle(appName) .setTicker(appName).setContentIntent(contentIntent); String message = extras.getString("message"); if (message != null) { mBuilder.setContentText(message); } else { mBuilder.setContentText("&lt;missing message content&gt;"); } String msgcnt = extras.getString("msgcnt"); if (msgcnt != null) { mBuilder.setNumber(Integer.parseInt(msgcnt)); } mNotificationManager.notify((String) appName, NOTIFICATION_ID, mBuilder.build()); tryPlayRingtone(); } private void tryPlayRingtone() { try { Uri notification = RingtoneManager .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); r.play(); } catch (Exception e) { Log.e(TAG, "failed to play notification ringtone"); } } public static void cancelNotification(Context context) { NotificationManager mNotificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.cancel((String) getAppName(context), NOTIFICATION_ID); } private static String getAppName(Context context) { CharSequence appName = context.getPackageManager().getApplicationLabel( context.getApplicationInfo()); return (String) appName; } public boolean isInForeground() { ActivityManager activityManager = (ActivityManager) getApplicationContext() .getSystemService(Context.ACTIVITY_SERVICE); List&lt;RunningTaskInfo&gt; services = activityManager .getRunningTasks(Integer.MAX_VALUE); if (services.get(0).topActivity .getPackageName() .toString() .equalsIgnoreCase( getApplicationContext().getPackageName().toString())) return true; return false; } @Override public void onError(Context context, String errorId) { Log.e(TAG, "onError - errorId: " + errorId); } </code></pre> <p>}</p></li> </ul> <h2>- PushHandlerActivity</h2> <pre><code>package com.ettamen.app.pushnotifications; import com.ettamen.app.PushNotificationWorker; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.util.Log; public class PushHandlerActivity extends Activity { private static String TAG = "PushHandlerActivity"; /* * this activity will be started if the user touches a notification that we * own. We send it's data off to the push plugin for processing. If needed, * we boot up the main activity to kickstart the application. * * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG, "onCreate"); boolean isPushPluginActive = PushNotificationWorker.isActive(); if (!isPushPluginActive) { forceMainActivityReload(); } // processPushBundle(isPushPluginActive); GCMIntentService.cancelNotification(this); finish(); } /** * Takes the pushBundle extras from the intent, and sends it through to the * PushPlugin for processing. */ private void processPushBundle(boolean isPushPluginActive) { // Bundle extras = getIntent().getExtras(); // // if (extras != null) { // // Bundle originalExtras = extras.getBundle("pushBundle"); // // if ( !isPushPluginActive ) { // originalExtras.putBoolean("coldstart", true); // } // // PushPlugin.sendExtras(originalExtras); // } } /** * Forces the main activity to re-launch if it's unloaded. */ private void forceMainActivityReload() { PackageManager pm = getPackageManager(); Intent launchIntent = pm .getLaunchIntentForPackage(getApplicationContext() .getPackageName()); startActivity(launchIntent); } } </code></pre> <h2>- My Registration code in the Main activity:</h2> <pre><code> private void getRegID(CallbackContext ctx) { new AsyncTask&lt;CallbackContext, Void, String&gt;() { CallbackContext context; @Override protected String doInBackground(CallbackContext... params) { // TODO Auto-generated method stub context = params[0]; String regid = ""; try { if (gcm == null) { gcm = GoogleCloudMessaging.getInstance(cordova .getActivity()); } regid = gcm.register(Constants.SENDER_ID); } catch (IOException ex) { Log.d("Exceptio:", ex.getMessage()); } String message = "{\"ChannelURI\":\"" + regid + "\"}"; return message; }; protected void onPostExecute(String result) { try { JSONObject jResult = new JSONObject(result); context.success(jResult); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.execute(ctx); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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