You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			60 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Dart
		
	
		
		
			
		
	
	
			60 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Dart
		
	
| 
								 
											3 years ago
										 
									 | 
							
								import 'dart:async';
							 | 
						||
| 
								 | 
							
								import 'dart:io';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import 'package:firebase_messaging/firebase_messaging.dart';
							 | 
						||
| 
								 | 
							
								import 'package:flutter/cupertino.dart';
							 | 
						||
| 
								 | 
							
								import 'package:flutter/material.dart';
							 | 
						||
| 
								 | 
							
								import 'package:mohem_flutter_app/app_state/app_state.dart';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// |--> Push Notification Background
							 | 
						||
| 
								 | 
							
								Future<dynamic> backgroundMessageHandler(message) async {
							 | 
						||
| 
								 | 
							
								  print("Firebase backgroundMessageHandler!!!");
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class PushNotificationHandler {
							 | 
						||
| 
								 | 
							
								  final BuildContext context;
							 | 
						||
| 
								 | 
							
								  static PushNotificationHandler? _instance;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  PushNotificationHandler(this.context) {
							 | 
						||
| 
								 | 
							
								    PushNotificationHandler._instance = this;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  static PushNotificationHandler getInstance() => _instance!;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  void init() async {
							 | 
						||
| 
								 | 
							
								    FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
							 | 
						||
| 
								 | 
							
								      if (Platform.isIOS) {
							 | 
						||
| 
								 | 
							
								        await Future.delayed(Duration(milliseconds: 3000)).then((value) {
							 | 
						||
| 
								 | 
							
								          newMessage(message);
							 | 
						||
| 
								 | 
							
								        });
							 | 
						||
| 
								 | 
							
								      } else {
							 | 
						||
| 
								 | 
							
								        newMessage(message);
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
							 | 
						||
| 
								 | 
							
								      if (Platform.isIOS) {
							 | 
						||
| 
								 | 
							
								        await Future.delayed(Duration(milliseconds: 3000)).then((value) {
							 | 
						||
| 
								 | 
							
								          newMessage(message);
							 | 
						||
| 
								 | 
							
								        });
							 | 
						||
| 
								 | 
							
								      } else {
							 | 
						||
| 
								 | 
							
								        newMessage(message);
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    FirebaseMessaging.instance.onTokenRefresh.listen((fcm_token) {
							 | 
						||
| 
								 | 
							
								      print("Push Notification onTokenRefresh: " + fcm_token);
							 | 
						||
| 
								 | 
							
								      AppState().setDeviceToken = fcm_token;
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  void newMessage(RemoteMessage remoteMessage) async {
							 | 
						||
| 
								 | 
							
								    print("Remote Message: " + remoteMessage.data.toString());
							 | 
						||
| 
								 | 
							
								    if (remoteMessage.data.isEmpty) {
							 | 
						||
| 
								 | 
							
								      return;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 |