Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
khaledmorshed opened this issue Jul 7, 2024 · 2 comments
Open

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

khaledmorshed opened this issue Jul 7, 2024 · 2 comments

Comments

@khaledmorshed
Copy link

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
Copy link
Author

f

@mohamad97-alhomsi
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants