在libGDX(Android和iOS项目)中,是否有推送通知的方法?

8

有人知道是否可以在使用RoboVM libGDX项目的Android和iOS中添加推送通知(例如Amazon Simple Notification Service)吗?如果可能的话,是否有任何好教程或良好的提示如何实现这些功能?

希望能够得到关于如何实现它的每一个提示。

2个回答

6

你好,我知道这是一个老问题,但是我一直在为此苦苦寻找解决方案,尤其是对于iOS平台。最终我找到了一种方法。如果下面的解释让您感到困惑,您可以查看以下Github仓库中的示例项目:

Github仓库

我只展示了iOS平台的代码,请查看仓库获取Android平台的代码。

这个想法很简单:你需要创建一个类来处理每个项目(Android和iOS)上的每个平台的通知发送,并实现一个名为“NotificationsHandler”的接口。

NotificationsHandler:

public interface NotificationsHandler {

    public void showNotification(String title, String text);
}

iOS适配器:

public class AdapteriOS implements NotificationsHandler {

    public AdapteriOS () {
        //Registers notifications, it will ask user if ok to receive notifications from this app, if user selects no then no notifications will be received
        UIApplication.getSharedApplication().registerUserNotificationSettings(UIUserNotificationSettings.create(UIUserNotificationType.Alert, null));
        UIApplication.getSharedApplication().registerUserNotificationSettings(UIUserNotificationSettings.create(UIUserNotificationType.Sound, null));
        UIApplication.getSharedApplication().registerUserNotificationSettings(UIUserNotificationSettings.create(UIUserNotificationType.Badge, null));

        //Removes notifications indicator in app icon, you can do this in a different way
        UIApplication.getSharedApplication().setApplicationIconBadgeNumber(0);
        UIApplication.getSharedApplication().cancelAllLocalNotifications();
    }


    @Override
    public void showNotification(final String title, final String text) {
        NSOperationQueue.getMainQueue().addOperation(new Runnable() {
            @Override
            public void run() {
                NSDate date = new NSDate();
                //5 seconds from now
                NSDate secondsMore = date.newDateByAddingTimeInterval(5);

                UILocalNotification localNotification = new UILocalNotification();
                localNotification.setFireDate(secondsMore);
                localNotification.setAlertBody(title);
                localNotification.setAlertAction(text);
                localNotification.setTimeZone(NSTimeZone.getDefaultTimeZone());
                localNotification.setApplicationIconBadgeNumber(UIApplication.getSharedApplication().getApplicationIconBadgeNumber() + 1);

                UIApplication.getSharedApplication().scheduleLocalNotification(localNotification);
            }
        });
    }
}

现在,默认情况下,Libgdx会将您的ApplicationListener或Game对象与配置对象一起传递给AndroidLauncher和IOSLauncher。技巧是将我们之前创建的类传递给ApplicationListener,以便您可以在Core项目中使用它。非常简单:
public class IOSLauncher extends IOSApplication.Delegate {

    @Override
    protected IOSApplication createApplication() {
        IOSApplicationConfiguration config = new IOSApplicationConfiguration();

        // This is your ApplicationListener or Game class
        // it will be called differently depending on what you 
        // set up when you created the libgdx project
        MainGame game = new MainGame();

        // We instantiate the iOS Adapter
        AdapteriOS adapter = new AdapteriOS();

        // We set the handler, you must create this method in your class
        game.setNotificationHandler(adapter);

        return new IOSApplication(game, config);
    }

    public static void main(String[] argv) {
        NSAutoreleasePool pool = new NSAutoreleasePool();
        UIApplication.main(argv, null, IOSLauncher.class);
        pool.close();
    }
}

现在你已经有了 NotificationHandler 实现的引用,可以通过你的核心项目直接调用它。

public class MainGame extends Game {

    // This is the notificatino handler
    public NotificationHandler notificationHandler;

    @Override
    public void create () {

        // Do whatever you do when your game is created
        // ...
    }

    @Override
    public void render () {
        super.render();

        // This is just an example but you
        // can now send notifications in your project
        if(condition)
            notificationHandler.showNotification("Title", "Content");
    }

    @Override
    public void dispose() {
        super.dispose();
    }

    // This is the method we created to set the notifications handler
    public void setNotificationHandler(NotificationHandler handler) {
        this.notificationHandler = handler;
    }
}

最后一件事

如果您需要运行桌面版,则需要为桌面版本做同样的操作,否则可能会出现错误,它将不会在桌面上执行任何操作,或者您可以在调用showNotfication方法之前检查平台。 您可以克隆我做这个的存储库:

Repo GitHub


1
太棒了,在那么长的时间后终于得到了答案...目前我已经放下了这个问题,不再使用任何类型的通知。也许我将在未来的某个应用程序中尝试这个功能。 我理解得对吗,这是本地通知的解决方案? - Joschka Schulz
是的,这是用于本地通知的。现在我再读一遍你的问题,你想要做推送通知,我的错。但我会尝试找到关于推送通知的内容并更新答案 :) - Leo

2
我从未亲身尝试过。但是,您可以使用此教程了解如何在libGDX项目中编写特定于Android的代码。然后,您的Android代码可以接收通知并在libGDX中触发回调。我希望这至少是朝着正确方向迈出的一步。
不过,我不确定是否可以对iOS做同样的事情。

谢谢,看起来这是一个不错的开始...我已经想到了iOS会带来更多的麻烦。 - Joschka Schulz
我的Android项目(debug-build)可以工作,但是release版本仍然有错误。如果有人能写一个iOS的教程,我想测试一下。 - mars3142
我在想这个是否可行: http://masl.cis.gvsu.edu/2012/01/31/java-client-for-appengine-channels/ - JohnyTex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接