Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

android - IBM Worklight 5.0.6.1 - Not getting Push Notifications when phone/app is closed

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...