Skip to content

Commit

Permalink
Working Notification
Browse files Browse the repository at this point in the history
  • Loading branch information
mukul2428 committed Aug 23, 2020
1 parent 103d22c commit e9909cc
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 60 deletions.
8 changes: 7 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@

</provider>


<!--for error in analytics!-->
<meta-data
android:name="firebase_messaging_auto_init_enabled"
android:value="false" />
<meta-data
android:name="firebase_analytics_collection_enabled"
android:value="false" />

<activity android:name=".SplashScreen">

Expand Down
16 changes: 11 additions & 5 deletions app/src/main/java/com/techexpert/indianvaarta/ChatActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ protected void onPause() {
MainActivity.UpdateUserStatus("offline");
}

// @Override
// protected void onDestroy() {
// MainActivity.UpdateUserStatus("offline");
// super.onDestroy();
// }

private void InitializeControllers()
{

Expand Down Expand Up @@ -691,7 +697,7 @@ public void onComplete(@NonNull Task task) {
{
Toast.makeText(ChatActivity.this, "Message Sent", Toast.LENGTH_SHORT).show();

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(messageSenderID);
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(messageSenderID);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot)
Expand All @@ -700,7 +706,7 @@ public void onDataChange(@NonNull DataSnapshot snapshot)
contacts user = snapshot.getValue(contacts.class);
if(notify)
{
sendNotification(messageReceiverID,user.getName(),MessageText);
sendNotification(messageReceiverID,user.getName(),MessageText, user.getName(), user.getImage());
}

notify = false;
Expand All @@ -724,7 +730,7 @@ public void onCancelled(@NonNull DatabaseError error) {
}
}

private void sendNotification(final String receiver, final String userName, final String message)
private void sendNotification(final String receiver, final String userName, final String message, final String Name, final String Image)
{
DatabaseReference tokens = FirebaseDatabase.getInstance().getReference("Tokens");
Query query = tokens.orderByKey().equalTo(receiver);
Expand All @@ -735,8 +741,8 @@ public void onDataChange(@NonNull DataSnapshot snapshot)
for(DataSnapshot snapshot1 : snapshot.getChildren())
{
Token token = snapshot1.getValue(Token.class);
Data data = new Data(messageSenderID,R.mipmap.ic_launcher,userName+": "+message,
"New Message", messageReceiverID);
Data data = new Data(messageSenderID,R.mipmap.ic_launcher,message,
Name, messageReceiverID, Name, Image);

Sender sender = new Sender(data, token.getToken());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ private void updateToken(String token)
{
currentUser = mAuth.getCurrentUser();
currentUserID = mAuth.getCurrentUser().getUid();
reference.child(currentUserID).setValue(token1);
}
reference.child(currentUserID).setValue(token1);

}


Expand All @@ -210,6 +211,7 @@ protected void onStart()
}
else
{
SharedPreferences();
UpdateUserStatus("online");
VerifyUserExistence();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.techexpert.indianvaarta.Notifications;

import com.techexpert.indianvaarta.Notifications.MyResponse;
import com.techexpert.indianvaarta.Notifications.Sender;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ public class Data
private String body;
private String title;
private String sent;
private String name;
private String pic;

public Data() {
}

public Data(String user, int icon, String body, String title, String sent) {
public Data(String user, int icon, String body, String title, String sent, String name, String pic) {
this.user = user;
this.icon = icon;
this.body = body;
this.title = title;
this.sent = sent;
this.name = name;
this.pic = pic;
}

public String getUser() {
Expand Down Expand Up @@ -58,4 +62,20 @@ public String getSent() {
public void setSent(String sent) {
this.sent = sent;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPic() {
return pic;
}

public void setPic(String pic) {
this.pic = pic;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.techexpert.indianvaarta.ChatActivity;
import com.techexpert.indianvaarta.MainActivity;
import com.techexpert.indianvaarta.R;

public class FirebaseMessaging extends FirebaseMessagingService
Expand All @@ -33,7 +36,9 @@ public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());

if (remoteMessage == null)
{
return;
}

String sent = remoteMessage.getData().get("sent");
String user = remoteMessage.getData().get("user");
Expand All @@ -43,6 +48,7 @@ public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
{

FirebaseUser firebaseUser = mAuth.getCurrentUser();

SharedPreferences sp = getSharedPreferences("SP_USER",MODE_PRIVATE);
String savedCurrentUser = sp.getString("Current_USERID","NONE");

Expand All @@ -69,14 +75,22 @@ private void sendNormalNotification(RemoteMessage remoteMessage)
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
String user = remoteMessage.getData().get("user");
String name = remoteMessage.getData().get("name");
String pic = remoteMessage.getData().get("pic");

RemoteMessage.Notification notification = remoteMessage.getNotification();
int j = Integer.parseInt(user.replaceAll("[\\D]",""));

Intent intent = new Intent(this, ChatActivity.class);

Bundle bundle = new Bundle();
bundle.putString("visit_user_id",user);
bundle.putString("visit_user_name",name);
bundle.putString("user_image",pic);

intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(this,j,intent,PendingIntent.FLAG_ONE_SHOT);

Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Expand Down Expand Up @@ -104,14 +118,22 @@ private void sendOAndAboveNotification(RemoteMessage remoteMessage)
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
String user = remoteMessage.getData().get("user");
String name = remoteMessage.getData().get("name");
String pic = remoteMessage.getData().get("pic");

RemoteMessage.Notification notification = remoteMessage.getNotification();
int j = Integer.parseInt(user.replaceAll("[\\D]",""));

Intent intent = new Intent(this, ChatActivity.class);

Bundle bundle = new Bundle();
bundle.putString("visit_user_id",user);
bundle.putString("visit_user_name",name);
bundle.putString("user_image",pic);

intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(this,j,intent,PendingIntent.FLAG_ONE_SHOT);

Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.net.Uri;
import android.os.Build;

import com.techexpert.indianvaarta.R;

public class NotificationOreo extends ContextWrapper
{

Expand Down Expand Up @@ -55,55 +57,7 @@ public Notification.Builder getNotifications(String title, String body, PendingI
.setContentText(body)
.setSound(soundUri)
.setAutoCancel(true)
.setSmallIcon(Integer.parseInt(icon));
.setSmallIcon(R.drawable.vaarta);
}

// @Override
// public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
// super.onMessageReceived(remoteMessage);
//
// String sent = remoteMessage.getData().get("sent");
//
// FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
//
// if(firebaseUser!=null && sent.equals(firebaseUser.getUid()))
// {
// sendNotification(remoteMessage);
// }
// }
//
// private void sendNotification(RemoteMessage remoteMessage)
// {
// String user = remoteMessage.getData().get("user");
// String icon = remoteMessage.getData().get("icon");
// String title = remoteMessage.getData().get("title");
// String body = remoteMessage.getData().get("body");
//
// RemoteMessage.Notification notification = remoteMessage.getNotification();
// int j = Integer.parseInt(user.replaceAll("[\\D]",""));
// Intent intent = new Intent(this, ChatActivity.class);
// Bundle bundle = new Bundle();
// bundle.putString("userid",user);
// intent.putExtras(bundle);
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// PendingIntent pendingIntent = PendingIntent.getActivity(this,j,intent,PendingIntent.FLAG_ONE_SHOT);
//
// Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
// .setSmallIcon(Integer.parseInt(icon))
// .setContentTitle(title)
// .setContentText(body)
// .setAutoCancel(true)
// .setSound(defaultSound)
// .setContentIntent(pendingIntent);
// NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//
// int i=0;
// if(j>0)
// {
// i=j;
// }
//
// notificationManager.notify(i,builder.build());
// }
}

0 comments on commit e9909cc

Please sign in to comment.