在libGDX(Android和iOS项目)中有推送通知的方法吗? [英] Is there a way for push notifications in libGDX (Android and iOS projects)?

查看:120
本文介绍了在libGDX(Android和iOS项目)中有推送通知的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Does someone knows if it is possible to add push notifications(like Amazon Simple Notification Service) in an Android and iOS with RoboVM libGDX projects? And if it is possible, are there any good tutorials or good hints how to implement such things?

我会很高兴每一个提示我如何实现它。

I would be happy about every hint how I can implement it.

推荐答案

嗨我知道这是一个老问题,但我很难找到专门针对iOS的解决方案,但我终于找到了办法。如果下面的解释令人困惑,你想在这里看到一个例子是带有示例项目的github仓库:

Hi I know this is an old question but I was struggling to find a solution for this specially for iOS, but I finally found a way. If the explanation below is confusing and you prefer to see an example here is a github repo with a sample project:

Repo GitHub

我只显示iOS的代码,请参阅Android的repo。

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

The idea is simple you need to create a class that handles sending a notification for each platform on each of your projects (Android and iOS) and have it implement an interface called 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项目中使用它。足够简单:

Now by default Libgdx passes your ApplicationListener or Game object to AndroidLauncher and IOSLauncher along with a configuration object. The trick is to pass the class we created earlier to the ApplicationListener so that you can use it inside your Core project. Simple enough:

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的实现您只需通过您的Core项目调用它。

Now that you have a reference to the implementation of NotificationHandler you can simply call it through your Core project.

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;
    }
}



最后一件事



如果您需要运行桌面版本,那么您需要为Desktop执行相同的操作,否则您可能会收到错误,它不会在桌面上执行任何操作,或者您可以在调用之前检查平台方法showNotfication。你可以在我这样做的地方克隆回购:

One last thing

If you need to run the Desktop version then you will need to do the same thing for Desktop otherwise you might get errors, it will not do anything on the Desktop, or you can check the platform before calling the method showNotfication. You can clone the repo where I do this:

Repo GitHub

这篇关于在libGDX(Android和iOS项目)中有推送通知的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆