-
Notifications
You must be signed in to change notification settings - Fork 226
/
OneSignal.dart
206 lines (173 loc) · 6.13 KB
/
OneSignal.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import 'package:flutter/material.dart';
import 'dart:async';
//import OneSignal
import 'package:onesignal/onesignal.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _debugLabelString = "";
String _emailAddress;
String _externalUserId;
bool _enableConsentButton = true;
// CHANGE THIS parameter to true if you want to test GDPR privacy consent
bool _requireConsent = true;
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
if (!mounted) return;
OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);
OneSignal.shared.setRequiresUserPrivacyConsent(_requireConsent);
var settings = {
OSiOSSettings.autoPrompt: false,
OSiOSSettings.promptBeforeOpeningPushUrl: true
};
OneSignal.shared.setNotificationReceivedHandler((notification) {
this.setState(() {
_debugLabelString =
"Received notification: \n${notification.jsonRepresentation().replaceAll("\\n", "\n")}";
});
});
OneSignal.shared
.setNotificationOpenedHandler((OSNotificationOpenedResult result) {
this.setState(() {
_debugLabelString =
"Opened notification: \n${result.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
});
});
OneSignal.shared
.setSubscriptionObserver((OSSubscriptionStateChanges changes) {
print("SUBSCRIPTION STATE CHANGED: ${changes.jsonRepresentation()}");
});
OneSignal.shared.setPermissionObserver((OSPermissionStateChanges changes) {
print("PERMISSION STATE CHANGED: ${changes.jsonRepresentation()}");
});
OneSignal.shared.setEmailSubscriptionObserver(
(OSEmailSubscriptionStateChanges changes) {
print("EMAIL SUBSCRIPTION STATE CHANGED ${changes.jsonRepresentation()}");
});
// NOTE: Replace with your own app ID from https://www.onesignal.com
await OneSignal.shared
.init("YOUR APP ID", iOSSettings: settings);
OneSignal.shared
.setInFocusDisplayType(OSNotificationDisplayType.notification);
bool requiresConsent = await OneSignal.shared.requiresUserPrivacyConsent();
this.setState(() {
_enableConsentButton = requiresConsent;
});
}
void _handlePromptForPushPermission() {
print("Prompting for Permission");
OneSignal.shared.promptUserForPushNotificationPermission().then((accepted) {
print("Accepted permission: $accepted");
});
}
void _handleGetPermissionSubscriptionState() {
print("Getting permissionSubscriptionState");
OneSignal.shared.getPermissionSubscriptionState().then((status) {
this.setState(() {
_debugLabelString = status.jsonRepresentation();
});
});
}
void _handleSendNotification() async {
var status = await OneSignal.shared.getPermissionSubscriptionState();
var playerId = status.subscriptionStatus.userId;
var imgUrlString =
"http://cdn1-www.dogtime.com/assets/uploads/gallery/30-impossibly-cute-puppies/impossibly-cute-puppy-2.jpg";
var notification = OSCreateNotification(
playerIds: [playerId],
content: "this is a test from OneSignal's Flutter SDK",
heading: "Test Notification",
iosAttachments: {"id1": imgUrlString},
bigPicture: imgUrlString,
buttons: [
OSActionButton(text: "test1", id: "id1"),
OSActionButton(text: "test2", id: "id2")
]);
var response = await OneSignal.shared.postNotification(notification);
this.setState(() {
_debugLabelString = "Sent notification with response: $response";
});
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Flutter and OneSignal'),
),
body: Container(
padding: EdgeInsets.all(10.0),
child: SingleChildScrollView(
child: new Table(
children: [
new TableRow(children: [
new OneSignalButton("Prompt for Push Permission",
_handlePromptForPushPermission, !_enableConsentButton)
]),
new TableRow(children: [
new OneSignalButton(
"Print Permission Subscription State",
_handleGetPermissionSubscriptionState,
!_enableConsentButton)
]),
new TableRow(children: [
Container(
height: 8.0,
)
]),
new TableRow(children: [
new OneSignalButton("Post Notification",
_handleSendNotification, !_enableConsentButton)
]),
new TableRow(children: [
new Container(
child: new Text(_debugLabelString),
alignment: Alignment.center,
)
]),
],
),
),
)),
);
}
}
typedef void OnButtonPressed();
class OneSignalButton extends StatefulWidget {
final String title;
final OnButtonPressed onPressed;
final bool enabled;
OneSignalButton(this.title, this.onPressed, this.enabled);
State<StatefulWidget> createState() => new OneSignalButtonState();
}
class OneSignalButtonState extends State<OneSignalButton> {
@override
Widget build(BuildContext context) {
return Table(
children: [
TableRow(children: [
RaisedButton.icon(
color: Colors.lightBlue,
icon: Icon(Icons.notifications),
textColor: Colors.white,
label: Text(widget.title),
onPressed: widget.enabled ? widget.onPressed : null,
)
]),
TableRow(children: [
Container(
height: 8.0,
)
]),
],
);
}
}