dkimitsa / robovm-robopods

32 stars 9 forks source link

OneSignal notifications are not working ('Push Notifications' capability missing) #33

Closed AndrewGDX closed 1 month ago

AndrewGDX commented 1 month ago

Hello! I have a problem with adding push notifications via OneSignal API to an iOS RoboVM LibGDX Project.

I tried this old manual and I managed to make build working with OneSignal v5.2.4. On launch I get Notifications permission request in iOS Simulator, but notifications are not working and I got 'Push Notifications capability missing' error. Do I need to add this capability to info.plist file somehow? I searched everywhere but there's no info about this.

Here is the full log:

OneSignal: This is new user
registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later.
WARNING: OneSignalUserManagerImpl.startNewSession() is unable to fetch user with External ID batbug1321 due to null OneSignal ID
OneSignal: This is new user
ERROR: ERROR! 'Push Notifications' capability missing! Add the capability in Xcode under 'Target' -> '<MyAppName(MainTarget)>' -> 'Signing & Capabilities' then click the '+ Capability' button.
WARNING: OSUserExecutor.executePendingRequests() is blocked by unexecutable request <OSRequestIdentifyUser with external_id: test>
OneSignal: User has accepted privacy policy

I have following folders in 'libs':

OneSignalCore.framework
OneSignalExtension.framework
OneSignalFramework.framework
OneSignalInAppMessages.framework
OneSignalLiveActivities.framework
OneSignalNotifications.framework
OneSignalOSCore.framework
OneSignalOutcomes.framework
OneSignalUser.framework
OneSignalNotificationServiceExtension.appex

my robovm.xml file:

    <frameworkPaths>
        <path>libs</path>
    </frameworkPaths>
    <frameworks>
        <framework>UIKit</framework>
        <framework>OpenGLES</framework>
        <framework>QuartzCore</framework>
        <framework>CoreGraphics</framework>
        <framework>OpenAL</framework>
        <framework>AudioToolbox</framework>
        <framework>AVFoundation</framework>
        <framework>OneSignalFramework</framework>
        <framework>OneSignalCore</framework>
        <framework>OneSignalOSCore</framework>
        <framework>OneSignalNotifications</framework>
        <framework>OneSignalOutcomes</framework>
        <framework>OneSignalUser</framework>
        <framework>OneSignalLiveActivities</framework>
        <framework>OneSignalExtension</framework>
        <framework>OneSignalInAppMessages</framework>
    </frameworks>
    <appExtensionPaths>
        <path>libs</path>
    </appExtensionPaths>
    <appExtensions>
        <extension>OneSignalNotificationServiceExtension</extension>
    </appExtensions>
</config>

and I added this to my info.plist.xml:

<key>UIBackgroundModes</key>
<array>
    <string>remote-notification</string>
</array>

code in IOSLauncher.java:

    @Override
    public boolean didFinishLaunching(UIApplication application, UIApplicationLaunchOptions launchOptions) {
      boolean success = super.didFinishLaunching(application, launchOptions);        
      // OneSignal initialization
      OneSignal.initialize(ONESIGNAL_API_KEY, null);
      OneSignal.login("test");

      // Check if the user has already granted notification permissions

      UNUserNotificationCenter currentNotificationCenter = UNUserNotificationCenter.currentNotificationCenter();
      currentNotificationCenter.getNotificationSettings((settings) -> {
            if (settings.getAuthorizationStatus() == UNAuthorizationStatus.Authorized){
              return;
            }

currentNotificationCenter.requestAuthorization(UNAuthorizationOptions.with(UNAuthorizationOptions.Alert, UNAuthorizationOptions.Sound, UNAuthorizationOptions.Badge),
              (granted, error) -> {
                    if (granted) {
                      UIApplication.getSharedApplication().registerForRemoteNotifications();
                    } else {
                      if (error != null) {
                       Gdx.app.error("Error requesting notification permission: ", error.getLocalizedDescription());
                       }
                      }
                    }
            );
        });

        return success;
    }

Do you have any ideas @dkimitsa? Thanks in advance.

dkimitsa commented 1 month ago

hello, it looks like you are missing Push Notification entitlement. if you run for device -- you should have it enabled in your provisioning profile. in case you are running on simulator it has to be enabled added to Entitlement.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>development</string>
</dict>
</plist>

if you use entitlement plist, make sure it is configured in robovm.xml:

entitlements.plist
AndrewGDX commented 1 month ago

Thank you very much, it's working perfectly now!

Just to keep all this documented, I want to point out the steps I took to make this happen.

  1. Download 9 OneSignal xcframework files from official repo
  2. Add each of them (ios-arm64_x86_64-simulator builds for testing and ios-arm64 for release) to libs folder in LibGDX project
  3. Build App Extension library in xcode - read in this article. You need to add all 9 frameworks in the project, and when building for iphoneos you need to use frameworks from ios-arm64 folder, and for iphonesimulator you use ios-arm64_x86_64-simulator frameworks
  4. Add resulting OneSignalNotificationServiceExtension.appex file to libs folder in your LibGDX project
  5. Add new code to robovm.xml, info.plist.xml, and IOSLauncher.java files like I mention above
  6. For testing environment add Entitlement.plist file and mention it in robovm.xml
  7. For release comment entitlements.plist in robovm.xml

These steps should be mentioned somewhere in the official samples, because all previous info about the process is very out of date and it took a long time for me to figure everything out.

dkimitsa commented 1 month ago

hi @AndrewGDX thanks for confirming things are still working, hadn't been using this pod since 2018 ) will add link to this comment to pods readme file during next update. thx