pusher / pusher-channels-flutter

Pusher Channels client library for Flutter targeting IOS, Android, and WEB
MIT License
74 stars 133 forks source link

notification not working when app is killed or terminated. #160

Open khaledmorshed opened 4 months ago

khaledmorshed commented 4 months ago

import 'dart:convert';

import 'package:erp/data/repositories/local/sharedpreferences/sharepreferences_class.dart'; import 'package:erp/providers/auth_provider/login_provider.dart'; import 'package:erp/providers/push_notification_provider/push_notification_provider.dart'; import 'package:erp/utils/global/navigation_service_without_context.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:provider/provider.dart'; import 'package:pusher_channels_flutter/pusher_channels_flutter.dart'; import 'package:shared_preferences/shared_preferences.dart';

import 'local_notification_service.dart';

String apiKey = "293f1afc5c963538c481"; //String apiKey = "c4887d89c118740fc0a1"; String cluster = "ap1"; //String cluster = "mt1"; String channelName = "notification-channel"; String userId = "1"; String title = "notification";

PusherChannelsFlutter pusher = PusherChannelsFlutter.getInstance();

class PushNotificationClass{ Future initializePushNotification(BuildContext context)async{ //method to set up push notification for global response // PusherChannelsFlutter pusher = PusherChannelsFlutter.getInstance();

try {
  await pusher.init(
    apiKey: apiKey,
    cluster: cluster,
    onConnectionStateChange: onConnectionStateChange,
    onError: onError,
    onSubscriptionSucceeded: onSubscriptionSucceeded,
    onEvent: onEvent,
    onSubscriptionError: onSubscriptionError,
    onDecryptionFailure: onDecryptionFailure,
    onMemberAdded: onMemberAdded,
    onMemberRemoved: onMemberRemoved,

  );
  await pusher.subscribe(channelName: channelName);
  await pusher.connect();

} catch (e) {
  print("ERROR: $e");
}

}

//when data will come this function will trigger void onEvent(PusherEvent event,) async{ // print("onEvent: $event"); String eventName = event.eventName; dynamic eventData = event.data; PermissionStatus status = await Permission.notification.status; if(PermissionStatus.granted == status && eventData.runtimeType == String){

  checkTokenDuringSendingNotification(eventData);
}

}

checkTokenDuringSendingNotification(dynamic payload){ Provider.of(NavigationService.navigatorKey.currentState!.context, listen: false).pushNotificationMap = payload == null ? null : jsonDecode(payload); Provider.of(NavigationService.navigatorKey.currentState!.context, listen: false).notifyListeners(); if(checkToken(Provider.of(NavigationService.navigatorKey.currentState!.context, listen: false).getUserToken())){ //this function show the notification on phone //here body is the main message LocalNotificationService().showNotification(/title: title,/ body: jsonDecode(payload)["data"], payLoad: payload); } }

//Checking token empty or not bool checkToken(dynamic token) { if (token == null || token == "") { return false; } else { return true; } }

void onSubscriptionSucceeded(String channelName, dynamic data) { print("onSubscriptionSucceeded: $channelName data: $data"); }

void onSubscriptionError(String message, dynamic e) { print("onSubscriptionError: $message Exception: $e"); }

void onDecryptionFailure(String event, String reason) { print("onDecryptionFailure: $event reason: $reason"); } static void onMemberAdded(String channelName, PusherMember member) { print("onMemberAdded: $channelName member: $member"); }

void onMemberRemoved(String channelName, PusherMember member) { print("onMemberRemoved: $channelName member: $member"); }

dynamic onAuthorizer(String channelName, String socketId, dynamic options) async { return { "auth": "foo:bar", "channel_data": '{"user_id": 1}', "shared_secret": "foobar" }; }

void onConnectionStateChange(dynamic currentState, dynamic previousState) async{ print("Connection state...1 $currentState"); // await Future.delayed(Duration(seconds: 5)); // reconnect(); if (currentState == ConnectionState.none) { print("currentState....2"); // Attempt to reconnect reconnect(); } }

void onError(String message, int? code, dynamic e) { print("onError: $message code: $code exception: $e"); }

}

khaledmorshed commented 4 months ago

f