Azure push notifications are not accepted when the Android application is closed

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); // Storing the registration id that indicates whether the generated token has been // sent to your server. If it is not stored, send the token to your server, // otherwise your server should have already received the token. if ((regID=sharedPreferences.getString("registrationID", null)) == null) { NotificationHub hub = new NotificationHub(NotificationSettings.HubName, NotificationSettings.HubListenConnectionString, this); Log.i(TAG, "Attempting to register with NH using token : " + token); regID = hub.register(token).getRegistrationId(); // If you want to use tags... // Refer to : https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-routing-tag-expressions/ // regID = hub.register(token, "tag1", "tag2").getRegistrationId(); resultString = "Registered Successfully - RegId : " + regID; Log.i(TAG, resultString); sharedPreferences.edit().putString("registrationID", regID ).apply(); } else { resultString = "Previously Registered Successfully - RegId : " + regID; } } catch (Exception e) { Log.e(TAG, resultString="Failed to complete token refresh", e); // If an exception happens while fetching the new token or updating our registration data // on a third-party server, this ensures that we'll attempt the update at a later time. } // Notify UI that registration has completed. if (MainActivity.isVisible) { MainActivity.mainActivity.ToastNotify(resultString); } } } 

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.

+6
source share
1 answer

I had a similar problem, not sure if the main reason is the same as yours.

The application will receive push notifications when the application was launched or in the background, but stopped receiving notifications when I killed the application from the task manager.

Turns out the problem was in my manifest. From the documentation ,

Your + ".permission.C2D_MESSAGE" permission to prevent other Android applications from registering and not receiving Android application messages. The permission name must exactly match this pattern, otherwise the Android application will not receive the message.

I defined my resolution as

 <permission android:name=".permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name=".permission.C2D_MESSAGE" /> 

The reason I used the notation . instead of the full package name, was that I used applicationIdSuffix for my debug build. I assumed that Gradle will correctly populate the package name based on the assembly type.

I made a mistake here. Apparently Gradle is not updating the package with a suffix in Permissions. To fix this, I replaced the code,

 <permission android:name="${applicationId}.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" /> 

In this case, the application began to receive notifications even after they were killed.

0
source

All Articles