- package com.cog.conferences.Notifications;
- import android.app.Notification;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.app.TaskStackBuilder;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.media.RingtoneManager;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Bundle;
- import android.support.v4.app.NotificationCompat;
- import android.util.Log;
- import androidx.annotation.RequiresApi;
- import com.cog.conferences.ChatContentActivity;
- import com.cog.conferences.MainActivity;
- import com.cog.conferences.R;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- import com.google.firebase.messaging.FirebaseMessagingService;
- import com.google.firebase.messaging.RemoteMessage;
- import java.util.Random;
- import static com.cog.conferences.R.drawable.ic_launcher_foreground;
- public class MyFirebaseIdServices extends FirebaseMessagingService {
- private static final String TAG = "MyFirebaseIdServices";
- @Override
- public void onNewToken(String s) {
- super.onNewToken(s);
- }
- @Override
- public void onMessageReceived(RemoteMessage remoteMessage) {
- super.onMessageReceived(remoteMessage);
- //Log.d("RRR", remoteMessage.toString());
- String sented = remoteMessage.getData().get("sented");
- //Log.d("sented",sented);
- String user = remoteMessage.getData().get("user");
- //Log.d("user",user);
- SharedPreferences preferences = getSharedPreferences("PREFS", MODE_PRIVATE);
- String currentUser = preferences.getString("currentuser","none");
- FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
- Log.d(TAG, "onMessageReceived: USERID -> " + firebaseUser.getUid());
- Log.d(TAG, "onMessageReceived: SENTED -> " + sented);
- if (sented != null && firebaseUser != null && sented.equals(firebaseUser.getUid())) {
- if (currentUser != null && !currentUser.equals(user)) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- sendOreoNotification(remoteMessage);
- } else {
- sendNotification(remoteMessage);
- }
- }
- }
- }
- private void sendOreoNotification(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]",""));
- Log.d("bu nedir", String.valueOf(j));
- Intent intent = new Intent(this, ChatContentActivity.class);
- Bundle bundle = new Bundle();
- bundle.putString("id",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);
- OreoNotification oreoNotification = new OreoNotification(this);
- Notification.Builder builder = oreoNotification.getOreoNotification(title,body,pendingIntent,defaultSound,icon);
- int i =0;
- if(j>0){
- Random rand = new Random();
- i= rand.nextInt(100000);
- }
- oreoNotification.getManager().notify(i,builder.build());
- }
- 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");
- Log.d(TAG, "sendNotification: " + remoteMessage);
- RemoteMessage.Notification notification = remoteMessage.getNotification();
- int j = Integer.parseInt(user.replaceAll("[\D]",""));
- Intent intent = new Intent(this, ChatContentActivity.class);
- Bundle bundle = new Bundle();
- bundle.putString("id",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(R.mipmap.ic_launcher)
- .setContentTitle(title)
- .setContentText(body)
- .setAutoCancel(true)
- .setColor(getResources().getColor(android.R.color.white))
- .setSound(defaultSound)
- .setContentIntent(pendingIntent)
- .setPriority(Notification.PRIORITY_MAX)
- .setDefaults(NotificationCompat.DEFAULT_VIBRATE);
- NotificationManager noti = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- int i =0;
- if(j>0){
- Random rand = new Random();
- i= rand.nextInt(100000);
- }
- noti.notify(i,builder.build());
- }
- }