[英]Is there a way for push notifications in libGDX (Android and iOS projects)?
有人知道是否可以在Android和iOS中使用RoboVM libGDX项目添加推送通知(如Amazon Simple Notification Service)? 如果有可能,是否有任何好的教程或良好的提示如何实现这些东西?
我会很高兴每一个提示我如何实现它。
嗨,我知道这是一个老问题,但我很难为iOS专门找到解决方案,但我终于找到了办法。 如果以下解释令人困惑,您希望在此处看到一个示例是带有示例项目的github仓库:
我只显示iOS的代码,看看Android的repo。
这个想法很简单,您需要创建一个类来处理在每个项目(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的实现,那么您只需通过Core项目调用它即可。
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版本,那么您需要为Desktop执行相同的操作,否则您可能会收到错误,它不会在桌面上执行任何操作,或者您可以在调用方法showNotfication之前检查平台。 您可以在我执行此操作时克隆repo:
我自己从来没有这样做过。 但您可以使用本教程了解如何在libGDX项目中编写Android特定代码。 然后,您的Android代码可以接收通知并在libGDX中触发回调。 我希望这至少是朝着正确方向迈出的一步。
但是我不确定为iOS做同样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.