IBM Worklight 5.0.6.1 - Not getting Push Notificat

2020-02-05 11:58发布

问题:

I have a Worklight hybrid app with basic push notification working in android. If the app is running and in focus when the notification is pushed, it behaves exactly as I would expect. The notification callback in my app is called, and it pops up a SimpleDialog. All Good.

If I dismiss the app by clicking on the Home Button, and a new message arrives, I see the notification in the Android notification area, and when I click on the item in the android notification list, the item gets dismissed from the list (but the app does not come back into focus) If I then launch my app from the Apps menu, it is sitting where I left it and the SimpleDialog is showing. (my notification handler was called) Mostly good, but I expected the app to come into focus when I selected the notification in the android notification list.

If I dismiss the app by clicking on the Back Button, and a new message arrives, I see the notification in the Android notification area, and when I click on the item in the android notification list, the item gets dismissed from the list (but the app does not come back into focus) If I then launch my app from the Apps menu, it launches the app fresh (I have to log in again) and my notification handler is never called. Not so good.

If I force stop the app, or turn the phone off while the notification is being sent (but leave the subscription in place), the notification never shows up on the phone. I don't see it in the Android Notification area when I restart the phone, and the notification handler in my app is never called when I launch the app. Very bad.

Is this the expected behavior?

I'm using Worklight 5.0.6.1, and I've seen this behavior on Android emulator at platform 4.2.2 and a physical phone at platform 4.1.2

EDT: Adding the code.

The adapter:

WL.Server.createEventSource({
    name : "MyPushEventSource",
    securityTest: "MyApp-strong-mobile-securityTest"
});

function submitNotification(userId) {

    var userSubscription = WL.Server.getUserNotificationSubscription(
            'MyPushNotification.MyPushEventSource', userId);

    if (userSubscription == null) {
        return {
            result : "No subscription found for user :: " + userId
        };
    }

    var notification = WL.Server
            .createDefaultNotification("There's work to be done!", 1, {});

    WL.Server.notifyAllDevices(userSubscription, notification);

    return {
        result : "Notification sent to user :: " + userId
    };
}

and in the app:

WL.Client.Push.onReadyToSubscribe = function() {

    var pushSubscribe_Success_Callback = function(response) {
        WL.Logger.debug("Enter: pushSubscribe_Success_Callback");
    };

    var pushSubscribe_Fail_Callback = function(response) {
        WL.Logger.debug("Enter: pushSubscribe_Fail_Callback");
    };

    var pushNotificationReceived = function(props, payload) {

        WL.SimpleDialog.show("Notification", props.alert, [
                { text : "OK" }]);
    };

    WL.Client.Push.registerEventSourceCallback("myPush",
            "MyPushNotification", "MyPushEventSource",
            pushNotificationReceived);

    if (!WL.Client.Push.isSubscribed("myPush")) {

        WL.Client.Push.subscribe("myPush", {
            onSuccess : pushSubscribe_Success_Callback,
            onFailure : pushSubscribe_Fail_Callback
        });
    }
 };

As I said, if the app is in focus, this all works without a hitch, so I know I have the Google messaging account and keys set up correctly. But for some reason I'm seeing unexpected results if the app isn't in focus when the notification is published.

回答1:

OK. It took me long enough to figure this one out, so in case anyone follows, here is the problem:

I had renamed the app by opening [android project]/res/values/strings.xml and changing the value of app_name.

That causes the app to not launch when a push notification for the app is selected.

I fixed this by changing app_name back to its original value, and adding a new String named app_label for the friendly name. I then went into AndroidManifest.xml, and changed the 2 instances of android:label="@string/app_name" to: android:label="@string/app_label" And sha-zam! My app had a friendly name AND clicking on notifications launched it.

For good measure I modified push_notification_title in strings.xml to be the friendly name, and that improved the display of the notification and did not break the launching of the app.



回答2:

All four scenarios work for me as expected.
Tested with Worklight v5.0.6.1 and the accompaning PushNotifications project.

After setting up the project with a GCM Key and GCM ID, deploying the adapter and application and launching the app on my Galaxy S4 running Android 4.2.2:

  1. I have logged-in, subscribed and sent a notification
  2. I have logged-in, subscribed, tapped the Home button to send the app to the background and sent a notification
  3. I have logged-in, suscribed, tapped the Back button to close the app and sent a notification
  4. I have logged-in, subscribed, closed the app, turned off the device

  1. While the app was in the foreground, the dialogs of the received notification were displayed
  2. While the app was in the background, I received the notification in the notification bar. Tapping it brought the app to the foreground and the dialogs of the received notification were displayed
  3. While the app was closed, I received the notification in the notification bar. Tapping it launched the app, after logging-in the dialogs of the received notification were displayed
  4. Turned on the device, waited for it to connect to the network, I received the notification in the notification bar. Tapping it it launched the app, after logging-in the dialogs of the received notification were displayed

If this is a push notifications app you have written yourself, I suggest that you review your code. You can use the sample app as guidance.