Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Live Activity integrated with OneSignal
- Loading branch information
1 parent
e9cf5dd
commit 891ed79
Showing
13 changed files
with
507 additions
and
43 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import Foundation | ||
|
||
import ActivityKit | ||
import Flutter | ||
import Foundation | ||
|
||
class LiveActivitiesManager { | ||
private static var callbackChannel: FlutterMethodChannel? = nil | ||
private static var managerChannel: FlutterMethodChannel? = nil | ||
|
||
public static func register(controller: FlutterViewController) { | ||
// Setup the iOS native->Flutter channel. This is primarily used to allow iOS to tell Flutter | ||
// when there is a new Live Activity update push token. | ||
// The name here must be equivalent to the name for `_callbackMethodChannel` in `lib/live_activities_manager.dart` | ||
callbackChannel = FlutterMethodChannel( | ||
name: "com.example.flutter_application_la/liveActivitiesCallback", | ||
binaryMessenger: controller.binaryMessenger | ||
) | ||
|
||
// Setup the Flutter->iOS native channel. Currently supports `startLiveActivity` but can be | ||
// expanded for more if needed. | ||
// The name here must be equivalent to the name for `_managerMethodChannel` in `lib/live_activities_manager.dart` | ||
managerChannel = FlutterMethodChannel( | ||
name: "com.example.flutter_application_la/liveActivitiesManager", | ||
binaryMessenger: controller.binaryMessenger | ||
) | ||
managerChannel?.setMethodCallHandler(handleMethodCall) | ||
} | ||
|
||
static func handleMethodCall(call: FlutterMethodCall, result: FlutterResult) { | ||
switch call.method { | ||
case "startLiveActivity": | ||
LiveActivitiesManager.startLiveActivity( | ||
data: call.arguments as? Dictionary<String,Any> ?? [String: Any](), | ||
result: result) | ||
break | ||
default: | ||
result(FlutterMethodNotImplemented) | ||
} | ||
} | ||
|
||
static func startLiveActivity(data: [String: Any], result: FlutterResult) { | ||
if #unavailable(iOS 16.1) { | ||
result(FlutterError(code: "1", message: "Live activity supported on 16.1 and higher", details: nil)) | ||
} | ||
|
||
let attributes = WidgetExtensionAttributes(name: data["name"] as? String ?? "LA Title") | ||
|
||
let state = WidgetExtensionAttributes.ContentState( | ||
emoji: data["emoji"] as? String ?? "😀" | ||
) | ||
|
||
if #available(iOS 16.1, *) { | ||
do { | ||
let newActivity = try Activity<WidgetExtensionAttributes>.request( | ||
attributes: attributes, | ||
contentState: state, | ||
pushType: .token) | ||
|
||
Task { | ||
for await pushToken in newActivity.pushTokenUpdates { | ||
let token = pushToken.map {String(format: "%02x", $0)}.joined() | ||
callbackChannel?.invokeMethod("updatePushTokenCallback", arguments: ["activityId": data["activityId"], "token": token ]) | ||
} | ||
} | ||
} catch let error { | ||
result(FlutterError(code: "2", message: "Error requesting live activity", details: nil)) | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
ios/WidgetExtension/Assets.xcassets/AccentColor.colorset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
ios/WidgetExtension/Assets.xcassets/AppIcon.appiconset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"platform" : "ios", | ||
"size" : "1024x1024" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
ios/WidgetExtension/Assets.xcassets/WidgetBackground.colorset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?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>NSExtension</key> | ||
<dict> | ||
<key>NSExtensionPointIdentifier</key> | ||
<string>com.apple.widgetkit-extension</string> | ||
</dict> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import WidgetKit | ||
import SwiftUI | ||
|
||
@main | ||
struct WidgetExtensionBundle: WidgetBundle { | ||
var body: some Widget { | ||
WidgetExtensionLiveActivity() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import ActivityKit | ||
import WidgetKit | ||
import SwiftUI | ||
|
||
struct WidgetExtensionAttributes: ActivityAttributes { | ||
public struct ContentState: Codable, Hashable { | ||
// Dynamic stateful properties about your activity go here! | ||
var emoji: String | ||
} | ||
|
||
// Fixed non-changing properties about your activity go here! | ||
var name: String | ||
} | ||
|
||
@available(iOS 16.1, *) | ||
struct WidgetExtensionLiveActivity: Widget { | ||
var body: some WidgetConfiguration { | ||
ActivityConfiguration(for: WidgetExtensionAttributes.self) { context in | ||
// Lock screen/banner UI goes here | ||
VStack { | ||
Text("Hello \(context.attributes.name)") | ||
Text(context.state.emoji) | ||
} | ||
.activityBackgroundTint(Color.cyan) | ||
.activitySystemActionForegroundColor(Color.black) | ||
|
||
} dynamicIsland: { context in | ||
DynamicIsland { | ||
// Expanded UI goes here. Compose the expanded UI through | ||
// various regions, like leading/trailing/center/bottom | ||
DynamicIslandExpandedRegion(.leading) { | ||
Text("Leading") | ||
} | ||
DynamicIslandExpandedRegion(.trailing) { | ||
Text("Trailing") | ||
} | ||
DynamicIslandExpandedRegion(.bottom) { | ||
Text("Bottom \(context.state.emoji)") | ||
} | ||
} compactLeading: { | ||
Text("L") | ||
} compactTrailing: { | ||
Text("T \(context.state.emoji)") | ||
} minimal: { | ||
Text(context.state.emoji) | ||
} | ||
.widgetURL(URL(string: "http://www.onesignal.com")) | ||
.keylineTint(Color.red) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import 'dart:developer'; | ||
import 'dart:async'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:onesignal_flutter/onesignal_flutter.dart'; | ||
|
||
// The flutter version of LiveActivitiesManager. The other side of these channels is in `ios/Runner/LiveActivitiesManager.swift`. | ||
class LiveActivitiesManager { | ||
static const MethodChannel _managerMethodChannel = MethodChannel('com.example.flutter_application_la/liveActivitiesManager'); | ||
static const MethodChannel _callbackMethodChannel = MethodChannel('com.example.flutter_application_la/liveActivitiesCallback'); | ||
|
||
static register() { | ||
_callbackMethodChannel.setMethodCallHandler(_handleCallback); | ||
} | ||
|
||
static Future<Null> _handleCallback(MethodCall call) async { | ||
var args = call.arguments.cast<String, dynamic>(); | ||
switch (call.method) { | ||
case 'updatePushTokenCallback': | ||
OneSignal.LiveActivities.enterLiveActivity(args["activityId"], args["token"]); | ||
default: | ||
log("Unrecognized callback method"); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
static Future<void> startLiveActivity(String activityId, String name, String emoji) async { | ||
try { | ||
await _managerMethodChannel.invokeListMethod('startLiveActivity', {'activityId': activityId, 'name': name, 'emoji': emoji}); | ||
} catch (e, st) { | ||
log(e.toString(), stackTrace: st); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters