Nejib Afdhal
Nejib Afdhal

Reputation: 69

how add action button to payload onesignal json apns file

I try to add action button in notification onesignal react-native ios.So i used apns file for test in simulator, the notification coming with dragging file but action button does not appear even i made a long press can anyone give me the correct aps format json and thinks for help this my payload apns file for test:

{
    "Simulator Target Bundle": "******",
    "aps": {
        "alert": {
            "title": "Push Notification",
            "subtitle": "Test Push Notifications",
            "body": "Testing Push Notifications on iOS Simulator",
            "actionButtons":[
                       {
                       "id":"test",
                       "text":"test"
                        }
                           ]
             
        }
    },
    "custom": {
        "i": "notificationId as UUID",
        "a": {"deeplinkKey": "{\"deeplinkDetailKey\":\"deeplinkDetailValue\"}", "launchURL": "example://collection/myCollectionId/type/1"}
    }
}

Upvotes: 2

Views: 311

Answers (2)

Nejib Afdhal
Nejib Afdhal

Reputation: 69

enter image description hereI solved my problem by adding these lines which include the category name and the action button to AppDelegate.m file

// Define notification actions (the buttons)
  UNNotificationAction *acceptAction = [UNNotificationAction actionWithIdentifier:@"accept" title:@"Accept" options:UNNotificationActionOptionForeground];
  UNNotificationAction *declineAction = [UNNotificationAction actionWithIdentifier:@"ignore" title:@"Decline" options:UNNotificationActionOptionDestructive];
  // Define the notification category with the actions
  UNNotificationCategory *inviteCategory = [UNNotificationCategory categoryWithIdentifier:@"myapp_action" actions:@[acceptAction, declineAction]
                                  intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];

Upvotes: 0

Pratik Prakash Bindage
Pratik Prakash Bindage

Reputation: 975

  • OneSignal does not support action buttons in iOS push notifications. You can achieve a similar effect by including a category in your payload. Here's an example of modifying your payload to add a category.

    {
     "Simulator Target Bundle": "******",
     "aps": {
         "alert": {
             "title": "Push Notification",
             "subtitle": "Test Push Notifications",
             "body": "Testing Push Notifications on iOS Simulator"
         },
         "category": "testCategory"
     },
     "custom": {
         "i": "notificationId as UUID",
         "a": {"deeplinkKey": "{\"deeplinkDetailKey\":\"deeplinkDetailValue\"}", "launchURL": "example://collection/myCollectionId/type/1"}
     }
    }
    
  • In your app's code, you can handle the category in the didReceiveRemoteNotification method of your UNUserNotificationCenterDelegate

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
     if response.notification.request.content.categoryIdentifier == "testCategory" {
         // Handle the category action here
     }
     completionHandler()
    }
    
  • User Notifications Push user-facing notifications to the user’s device from a server, or generate them locally from your app : https://developer.apple.com/documentation/usernotifications

Upvotes: 1

Related Questions