|
1 | | -import 'dart:io'; |
2 | | - |
3 | 1 | import 'package:flutter/material.dart'; |
4 | | -import 'package:qonversion_example/constants.dart'; |
5 | 2 |
|
6 | | -import 'handling_notification.dart'; |
7 | 3 | import 'home.dart'; |
8 | 4 | import 'params_view.dart'; |
9 | 5 | import 'products_view.dart'; |
10 | 6 | import 'dart:async'; |
11 | 7 |
|
12 | | -import 'package:firebase_core/firebase_core.dart'; |
13 | | -import 'package:firebase_messaging/firebase_messaging.dart'; |
14 | | -import 'package:flutter/foundation.dart'; |
15 | | -import 'package:flutter_local_notifications/flutter_local_notifications.dart'; |
16 | | -import 'dart:convert'; |
17 | | - |
18 | | -/// Define a top-level named handler which background/terminated messages will |
19 | | -/// call. |
20 | | -Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { |
21 | | - print('Handling a background message ${message.messageId}'); |
22 | | - showNotification(message); |
23 | | -} |
24 | | - |
25 | | -/// Initialize the [FlutterLocalNotificationsPlugin] package. |
26 | | -late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin; |
27 | | - |
28 | | -/// Create a [AndroidNotificationChannel] for heads up notifications |
29 | | -late AndroidNotificationChannel channel; |
30 | | - |
31 | 8 | Future<void> main() async { |
32 | 9 | WidgetsFlutterBinding.ensureInitialized(); |
33 | | - await Firebase.initializeApp(); |
34 | | - |
35 | | - // Set the background messaging handler early on, as a named top-level function |
36 | | - FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); |
37 | | - |
38 | | - if (!kIsWeb) { |
39 | | - channel = const AndroidNotificationChannel( |
40 | | - Constants.channelId, // id |
41 | | - Constants.channelName, // name |
42 | | - importance: Importance.high, |
43 | | - ); |
44 | | - |
45 | | - /// Configure FlutterLocalNotificationsPlugin |
46 | | - flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); |
47 | | - var initializationSettingsAndroid = |
48 | | - AndroidInitializationSettings('launch_background'); |
49 | | - var initializationSettingsIOS = DarwinInitializationSettings( |
50 | | - requestAlertPermission: true, |
51 | | - requestBadgePermission: true, |
52 | | - requestSoundPermission: false); |
53 | | - var initializationSettings = InitializationSettings( |
54 | | - android: initializationSettingsAndroid, iOS: initializationSettingsIOS); |
55 | | - |
56 | | - await flutterLocalNotificationsPlugin.initialize(initializationSettings, |
57 | | - onDidReceiveNotificationResponse: (NotificationResponse details) async { |
58 | | - var notificationData = jsonDecode(details.payload ?? "{}"); |
59 | | - onNotificationClick(notificationData); |
60 | | - }); |
61 | | - |
62 | | - /// Create an Android Notification Channel. |
63 | | - await flutterLocalNotificationsPlugin |
64 | | - .resolvePlatformSpecificImplementation< |
65 | | - AndroidFlutterLocalNotificationsPlugin>() |
66 | | - ?.createNotificationChannel(channel); |
67 | | - |
68 | | - /// Update the iOS foreground notification presentation options to allow heads up notifications. |
69 | | - await FirebaseMessaging.instance |
70 | | - .setForegroundNotificationPresentationOptions( |
71 | | - alert: true, |
72 | | - badge: true, |
73 | | - sound: true, |
74 | | - ); |
75 | | - } |
76 | | - /// Grant entitlements for iOS |
77 | | - if (Platform.isIOS) { |
78 | | - NotificationSettings settings = |
79 | | - await FirebaseMessaging.instance.requestPermission( |
80 | | - announcement: true, |
81 | | - carPlay: true, |
82 | | - criticalAlert: true, |
83 | | - ); |
84 | | - |
85 | | - if (settings.authorizationStatus == AuthorizationStatus.authorized) { |
86 | | - print('User granted permission'); |
87 | | - } else if (settings.authorizationStatus == |
88 | | - AuthorizationStatus.provisional) { |
89 | | - print('User granted provisional permission'); |
90 | | - } else { |
91 | | - print('User declined or has not accepted permission'); |
92 | | - } |
93 | | - } |
94 | 10 |
|
95 | 11 | runApp(SampleApp()); |
96 | 12 | } |
|
0 commit comments