Skip to content

Commit

Permalink
push-notification
Browse files Browse the repository at this point in the history
  • Loading branch information
rudramistry001 committed Mar 15, 2024
1 parent 051bfc7 commit f7c9f50
Show file tree
Hide file tree
Showing 7 changed files with 376 additions and 243 deletions.
82 changes: 81 additions & 1 deletion login/lib/api/apis.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// ignore_for_file: avoid_print

import 'dart:convert';
import 'dart:io';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:http/http.dart';
import 'package:login/model/chat_user_model.dart';
import 'package:login/model/messages.dart';

Expand All @@ -23,6 +26,58 @@ class APIs {
//for returning current user
static User get user => auth.currentUser!;

// for accessing firebase messaging (Push Notification)
static FirebaseMessaging fMessaging = FirebaseMessaging.instance;

// for getting firebase messaging token
static Future<void> getFirebaseMessagingToken() async {
await fMessaging.getToken().then((t) {
if (t != null) {
me.PushToken = t;
print('Push Token: $t');
}
});

// for handling foreground messages
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');

if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
}

// for sending push notification
static Future<void> sendPushNotification(
ChatUser chatUser, String msg) async {
try {
final body = {
"to": chatUser.PushToken,
"notification": {
"title": me.Name, //our name should be send
"body": msg,
},
// "data": {
// "some_data": "User ID: ${me.id}",
// },
};

var res = await post(Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: {
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.authorizationHeader:
'key=AAAAJoFEO6I:APA91bElgw77iGQYJdEov_3ea5i3j6_vpjpPm7q4D4xahUmOPny_4W9g3Vus_FgrldMScGNcd7qn46lko8wTYsX-yZQsJhRrKaj5T_NV3TkJ8vLkMIxxkSKLuz2mwbNelOCo-t7ma-3N'
},
body: jsonEncode(body));
print('Response status: ${res.statusCode}');
print('Response body: ${res.body}');
} catch (e) {
print('\nsendPushNotificationE: $e');
}
}

//for checking if user exists or not??
static Future<bool> userExists() async {
return (await firestore.collection('Users').doc(user.uid).get()).exists;
Expand All @@ -33,6 +88,9 @@ class APIs {
await firestore.collection('Users').doc(user.uid).get().then((user) async {
if (user.exists) {
me = ChatUser.fromJson(user.data()!);
await getFirebaseMessagingToken();

APIs.updateActiveStatus(true);
} else {
await createUser().then((value) => getSelfInfo());
}
Expand Down Expand Up @@ -119,6 +177,27 @@ class APIs {
.snapshots();
}

// // for sending message
// static Future<void> sendMessage(
// ChatUser chatUser, String msg, Type type) async {
// //message sending time (also used as id)
// final time = DateTime.now().millisecondsSinceEpoch.toString();

// //message to send
// final Message message = Message(
// toId: chatUser.Id,
// msg: msg,
// read: '',
// type: type,
// fromId: user.uid,
// sent: time);

// final ref = firestore
// .collection('chats/${getConversationID(chatUser.Id)}/messages/');
// await ref.doc(time).set(message.toJson()).then((value) =>
// sendPushNotification(chatUser, type == Type.text ? msg : 'image'));
// }

// for sending message
static Future<void> sendMessage(
ChatUser chatUser,
Expand All @@ -139,7 +218,8 @@ class APIs {

final ref = firestore
.collection('chats/${getConversationID(chatUser.Id)}/messages/');
await ref.doc(time).set(message.toJson());
await ref.doc(time).set(message.toJson()).then((value) =>
sendPushNotification(chatUser, type == Type.text ? msg : 'image'));;
}

//update read status of message
Expand Down
2 changes: 1 addition & 1 deletion login/lib/auth/loginscreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class _LoginPageState extends State<LoginPage> {
print(
'Password: ${passwordController.text}');
_signIn();
APIs.updateActiveStatus(true);

},
label: Text(
"Login",
Expand Down
19 changes: 16 additions & 3 deletions login/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
Expand All @@ -10,15 +11,27 @@ import 'firebase_options.dart';
// global object for accessing device screen size
late Size mq;

void main() {
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
_initialiseFirebase();
await _initialiseFirebase();
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
const MyApp({super.key});

@override
State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
static FirebaseMessaging fMessaging = FirebaseMessaging.instance;
@override
void initState() {
super.initState();
fMessaging.requestPermission();
}

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
Expand Down
Loading

0 comments on commit f7c9f50

Please sign in to comment.