You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class PushNotificationClass{
Future initializePushNotification(BuildContext context)async{
//method to set up push notification for global response
// PusherChannelsFlutter pusher = PusherChannelsFlutter.getInstance();
//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;
}
}
You're right you have to use Firebase messaging to handle the notification when the app is terminated and that is not appropriate because you have to tell the backend developer to forward the notification to the Firebase
But in my country firebase is not available so I used the pusher package as an alternative for the notifications but if the pusher disconnected after the app is terminated it's useless to use the pusher for that
I hope they notice your issue and fix this in the next update
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();
}
//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(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");
}
}
The text was updated successfully, but these errors were encountered: