For quick information, I use the following tutorial:
https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-android-push-notification-google-gcm-get-started/
Problem I can receive push notifications (sent from my Nodejs server) when the application is open. However, when I close the application and remove it from the retentates, I do not receive any push notifications. I use the Azure Push notification hub.
All code is in the link above, however I also put it here as I implemented it.
The following snippets were added to the Android manifest file: (tag not shown for simplicity, I added pieces in the right places)
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <service android:name="<your package>.MyInstanceIDService" android:exported="false"> <intent-filter> <action android:name="com.google.android.gms.iid.InstanceID"/> </intent-filter> </service> <service android:name="<your package>.RegistrationIntentService" android:exported="false"> </service> <receiver android:name="com.microsoft.windowsazure.notifications.NotificationsBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="<your package name>" /> </intent-filter> </receiver> <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="<your package>.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="<your package>.permission.C2D_MESSAGE"/>
The following are the classes that I added in accordance with the tutorial:
public class NotificationSettings { public static String SenderId = "<Your project number>"; public static String HubName = "<Your HubName>"; public static String HubListenConnectionString = "<Your default listen connection string>"; }
InstanceID Listener Service:
import android.content.Intent; import android.util.Log; import com.google.android.gms.iid.InstanceIDListenerService; public class MyInstanceIDService extends InstanceIDListenerService { private static final String TAG = "MyInstanceIDService"; @Override public void onTokenRefresh() { Log.i(TAG, "Refreshing GCM Registration Token"); Intent intent = new Intent(this, RegistrationIntentService.class); startService(intent); } };
Class RegistrationIntentService:
import android.app.IntentService; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.util.Log; import com.google.android.gms.gcm.GoogleCloudMessaging; import com.google.android.gms.iid.InstanceID; import com.microsoft.windowsazure.messaging.NotificationHub; public class RegistrationIntentService extends IntentService { private static final String TAG = "RegIntentService"; private NotificationHub hub; public RegistrationIntentService() { super(TAG); } @Override protected void onHandleIntent(Intent intent) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); String resultString = null; String regID = null; try { InstanceID instanceID = InstanceID.getInstance(this); String token = instanceID.getToken(NotificationSettings.SenderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE); Log.i(TAG, "Got GCM Registration Token: " + token);
And the notification handler:
public class MyHandler extends NotificationsHandler { public static final int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; NotificationCompat.Builder builder; Context ctx; @Override public void onReceive(Context context, Bundle bundle) { ctx = context; String nhMessage = bundle.getString("message"); sendNotification(nhMessage); if (MainActivity.isVisible) { MainActivity.mainActivity.ToastNotify(nhMessage); } } private void sendNotification(String msg) { Intent intent = new Intent(ctx, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Notification Hub Demo") .setStyle(new NotificationCompat.BigTextStyle() .bigText(msg)) .setSound(defaultSoundUri) .setContentText(msg); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); } }
There are some more code snippets that I have not included here, but they are visible in the link above.
Please let me know if there are any special permissions for listening to push notifications when the application is not running.
EDIT
I think that if we somehow keep this onReceive function, even when the application is closed, we can achieve this. Still not sure how to do this.