|
| 1 | +import 'dart:convert'; |
| 2 | +import 'package:http/http.dart' as http; |
| 3 | +import 'package:firebase_messaging/firebase_messaging.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; |
| 6 | + |
| 7 | +class LocalNotificationService { |
| 8 | + static final FlutterLocalNotificationsPlugin _notificationsPlugin = |
| 9 | + FlutterLocalNotificationsPlugin(); |
| 10 | + |
| 11 | +// after this create a method initialize to initialize localnotification |
| 12 | + |
| 13 | + static void initialize() { |
| 14 | + // initializationSettings for Android |
| 15 | + const InitializationSettings initializationSettings = |
| 16 | + InitializationSettings( |
| 17 | + android: AndroidInitializationSettings("@mipmap/ic_launcher"), |
| 18 | + ); |
| 19 | + |
| 20 | + _notificationsPlugin.initialize( |
| 21 | + initializationSettings, |
| 22 | + // onDidReceiveNotificationResponse: (String? id) async { |
| 23 | + // print("onSelectNotification"); |
| 24 | + // if (id!.isNotEmpty) { |
| 25 | + // print("Router Value1234 $id"); |
| 26 | + |
| 27 | + // // Navigator.of(context).push( |
| 28 | + // // MaterialPageRoute( |
| 29 | + // // builder: (context) => DemoScreen( |
| 30 | + // // id: id, |
| 31 | + // // ), |
| 32 | + // // ), |
| 33 | + // // ); |
| 34 | + |
| 35 | + // } |
| 36 | + // }, |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + static sendPushMessage(String body, String title, String token) async { |
| 41 | + try { |
| 42 | + print('sending'); |
| 43 | + final response = |
| 44 | + await http.post(Uri.parse('https://fcm.googleapis.com/fcm/send'), |
| 45 | + headers: { |
| 46 | + 'Content-Type': 'application/json', |
| 47 | + 'Authorization': |
| 48 | + 'key= AAAAcJG6v-U:APA91bE1u0R-KyQlfI6D0j9Ku216LBHV78xxdSfRg2j-7R280EBsTNmB4Vgmw4TxTAjbqMj7RM7Estb12sYXQCIxOrSczzgpC9BcueEnIaW4Gq0NTynIQV20rnSw0hVD_3yGqzAOkKnT ', |
| 49 | + }, |
| 50 | + body: json.encode({ |
| 51 | + "pirority": "high", |
| 52 | + "registration_ids": [ |
| 53 | + token, |
| 54 | + // "cxDaTRu1T-eMCVEBycQAuj:APA91bFHXtHF20bGBRNVOcNKJ9hpxpSdjBbWL9QN0Es79hi4i_VpBlUYu8aNNHDovxibTvnIdu5EPmd6-85qbgtFUimiim6FfwTeieehy8_sUs_w7UNacJYRFq9wxWJVnEfVy9KhVhfM" |
| 55 | + ], |
| 56 | + "notification": { |
| 57 | + "body": body, |
| 58 | + "title": title, |
| 59 | + "android_channel_id": "flutterNotification", |
| 60 | + "sound": false |
| 61 | + } |
| 62 | + })); |
| 63 | + print('sent ${response.statusCode}and ${response.request}'); |
| 64 | + } catch (e) { |
| 65 | + print(e.toString()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | +/////////////////////////for audio///////////////////////////////// |
| 70 | + static sendPushAudio( |
| 71 | + String body, String title, String token, String imageurl) async { |
| 72 | + try { |
| 73 | + print('sending'); |
| 74 | + final response = |
| 75 | + await http.post(Uri.parse('https://fcm.googleapis.com/fcm/send'), |
| 76 | + headers: { |
| 77 | + 'Content-Type': 'application/json', |
| 78 | + 'Authorization': |
| 79 | + 'key= AAAAcJG6v-U:APA91bE1u0R-KyQlfI6D0j9Ku216LBHV78xxdSfRg2j-7R280EBsTNmB4Vgmw4TxTAjbqMj7RM7Estb12sYXQCIxOrSczzgpC9BcueEnIaW4Gq0NTynIQV20rnSw0hVD_3yGqzAOkKnT ', |
| 80 | + }, |
| 81 | + body: json.encode({ |
| 82 | + "pirority": "high", |
| 83 | + "registration_ids": [ |
| 84 | + token, |
| 85 | + // "cxDaTRu1T-eMCVEBycQAuj:APA91bFHXtHF20bGBRNVOcNKJ9hpxpSdjBbWL9QN0Es79hi4i_VpBlUYu8aNNHDovxibTvnIdu5EPmd6-85qbgtFUimiim6FfwTeieehy8_sUs_w7UNacJYRFq9wxWJVnEfVy9KhVhfM" |
| 86 | + ], |
| 87 | + "notification": { |
| 88 | + "body": body, |
| 89 | + "title": title, |
| 90 | + "image": imageurl, |
| 91 | + "android_channel_id": "flutterNotification", |
| 92 | + "sound": false |
| 93 | + } |
| 94 | + })); |
| 95 | + print('sent ${response.statusCode}and ${response.request}'); |
| 96 | + } catch (e) { |
| 97 | + print(e.toString()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | +/////////////////////////////////////////////////////////// |
| 102 | + static sendPushImage( |
| 103 | + String body, String title, String token, String imageUrl) async { |
| 104 | + try { |
| 105 | + print('sending'); |
| 106 | + final response = |
| 107 | + await http.post(Uri.parse('https://fcm.googleapis.com/fcm/send'), |
| 108 | + headers: { |
| 109 | + 'Content-Type': 'application/json', |
| 110 | + 'Authorization': |
| 111 | + 'key= AAAAcJG6v-U:APA91bE1u0R-KyQlfI6D0j9Ku216LBHV78xxdSfRg2j-7R280EBsTNmB4Vgmw4TxTAjbqMj7RM7Estb12sYXQCIxOrSczzgpC9BcueEnIaW4Gq0NTynIQV20rnSw0hVD_3yGqzAOkKnT ', |
| 112 | + }, |
| 113 | + body: json.encode({ |
| 114 | + "pirority": "high", |
| 115 | + "registration_ids": [ |
| 116 | + token, |
| 117 | + // "cxDaTRu1T-eMCVEBycQAuj:APA91bFHXtHF20bGBRNVOcNKJ9hpxpSdjBbWL9QN0Es79hi4i_VpBlUYu8aNNHDovxibTvnIdu5EPmd6-85qbgtFUimiim6FfwTeieehy8_sUs_w7UNacJYRFq9wxWJVnEfVy9KhVhfM" |
| 118 | + ], |
| 119 | + "notification": { |
| 120 | + "body": body, |
| 121 | + "title": title, |
| 122 | + "image": imageUrl, |
| 123 | + "android_channel_id": "flutterNotification", |
| 124 | + "sound": false |
| 125 | + } |
| 126 | + })); |
| 127 | + print('sent ${response.statusCode}and ${response.request}'); |
| 128 | + } catch (e) { |
| 129 | + print(e.toString()); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | +/////////////////////////////////////////////////////////// |
| 134 | + static void createanddisplaynotification(RemoteMessage message) async { |
| 135 | + try { |
| 136 | + final id = DateTime.now().millisecondsSinceEpoch ~/ 1000; |
| 137 | + const NotificationDetails notificationDetails = NotificationDetails( |
| 138 | + android: AndroidNotificationDetails( |
| 139 | + "flutterNotification", |
| 140 | + "flutterNotificationchannel", |
| 141 | + importance: Importance.max, |
| 142 | + priority: Priority.high, |
| 143 | + color: Colors.blue, |
| 144 | + colorized: true, |
| 145 | + ), |
| 146 | + ); |
| 147 | + |
| 148 | + await _notificationsPlugin.show( |
| 149 | + id, |
| 150 | + message.notification!.title, |
| 151 | + message.notification!.body, |
| 152 | + notificationDetails, |
| 153 | + payload: message.data['_id'], |
| 154 | + ); |
| 155 | + } on Exception catch (e) { |
| 156 | + print(e); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments