有人知道是否可以在使用RoboVM libGDX项目的Android和iOS中添加推送通知(例如Amazon Simple Notification Service)吗?如果可能的话,是否有任何好教程或良好的提示如何实现这些功能?
希望能够得到关于如何实现它的每一个提示。
有人知道是否可以在使用RoboVM libGDX项目的Android和iOS中添加推送通知(例如Amazon Simple Notification Service)吗?如果可能的话,是否有任何好教程或良好的提示如何实现这些功能?
希望能够得到关于如何实现它的每一个提示。
你好,我知道这是一个老问题,但是我一直在为此苦苦寻找解决方案,尤其是对于iOS平台。最终我找到了一种方法。如果下面的解释让您感到困惑,您可以查看以下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);
}
});
}
}
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方法之前检查平台。 您可以克隆我做这个的存储库: