2

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.

4

2 回答 2

7

好的。我花了足够长的时间才弄清楚这一点,所以如果有人关注,问题就在这里:

我通过打开 [android project]/res/values/strings.xml 并更改 app_name 的值重命名了该应用程序。

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

我通过将 app_name 改回其原始值并为友好名称添加一个名为 app_label 的新字符串来解决此问题。然后我进入 AndroidManifest.xml,并将 android:label="@string/app_name" 的 2 个实例更改为: android:label="@string/app_label" 和 sha-zam!我的应用程序有一个友好的名称,点击通知启动它。

为了更好地衡量,我将 strings.xml 中的 push_notification_title 修改为友好名称,这改善了通知的显示并且不会破坏应用程序的启动。

于 2013-08-15T04:52:43.470 回答
0

所有四种情况都按预期对我有用。
使用 Worklight v5.0.6.1 和随附的 PushNotifications 项目进行了测试。

使用 GCM 密钥和 GCM ID 设置项目后,部署适配器和应用程序并在运行 Android 4.2.2 的 Galaxy S4 上启动应用程序:

  1. 我已登录、订阅并发送了通知
  2. 我已登录、订阅、点击主页按钮将应用程序发送到后台并发送通知
  3. 我已登录、订阅、点击返回按钮关闭应用程序并发送通知
  4. 我已登录、订阅、关闭应用、关闭设备

  1. 当应用程序在前台时,显示收到通知的对话框
  2. 当应用程序在后台时,我在通知栏中收到了通知。点击它会将应用程序带到前台,并显示收到通知的对话框
  3. 当应用程序关闭时,我在通知栏中收到了通知。点击它启动应用程序,登录后显示收到通知的对话框
  4. 打开设备,等待它连接到网络,我在通知栏中收到了通知。点击它启动应用程序,登录后显示收到通知的对话框

如果这是您自己编写的推送通知应用程序,我建议您查看您的代码。您可以使用示例应用程序作为指导。

于 2013-07-11T06:00:08.017 回答