Facebook
From Big Bird, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 72
  1. package com.cog.conferences.Notifications;
  2.  
  3. import android.app.Notification;
  4. import android.app.NotificationChannel;
  5. import android.app.NotificationManager;
  6. import android.app.PendingIntent;
  7. import android.app.TaskStackBuilder;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.SharedPreferences;
  11. import android.media.RingtoneManager;
  12. import android.net.Uri;
  13. import android.os.Build;
  14. import android.os.Bundle;
  15. import android.support.v4.app.NotificationCompat;
  16. import android.util.Log;
  17.  
  18. import androidx.annotation.RequiresApi;
  19.  
  20. import com.cog.conferences.ChatContentActivity;
  21. import com.cog.conferences.MainActivity;
  22. import com.cog.conferences.R;
  23. import com.google.firebase.auth.FirebaseAuth;
  24. import com.google.firebase.auth.FirebaseUser;
  25. import com.google.firebase.messaging.FirebaseMessagingService;
  26. import com.google.firebase.messaging.RemoteMessage;
  27.  
  28. import java.util.Random;
  29.  
  30. import static com.cog.conferences.R.drawable.ic_launcher_foreground;
  31.  
  32. public class MyFirebaseIdServices extends FirebaseMessagingService {
  33.     private static final String TAG = "MyFirebaseIdServices";
  34.  
  35.     @Override
  36.     public void onNewToken(String s) {
  37.         super.onNewToken(s);
  38.     }
  39.  
  40.     @Override
  41.     public void onMessageReceived(RemoteMessage remoteMessage) {
  42.         super.onMessageReceived(remoteMessage);
  43.  
  44.         //Log.d("RRR", remoteMessage.toString());
  45.         String sented = remoteMessage.getData().get("sented");
  46.         //Log.d("sented",sented);
  47.         String user = remoteMessage.getData().get("user");
  48.         //Log.d("user",user);
  49.         SharedPreferences preferences = getSharedPreferences("PREFS", MODE_PRIVATE);
  50.         String currentUser = preferences.getString("currentuser","none");
  51.  
  52.         FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
  53.  
  54.         Log.d(TAG, "onMessageReceived: USERID -> " + firebaseUser.getUid());
  55.         Log.d(TAG, "onMessageReceived: SENTED -> " + sented);
  56.  
  57.  
  58.  
  59.         if (sented != null && firebaseUser != null && sented.equals(firebaseUser.getUid())) {
  60.             if (currentUser != null && !currentUser.equals(user)) {
  61.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  62.                 sendOreoNotification(remoteMessage);
  63.             } else {
  64.                 sendNotification(remoteMessage);
  65.             }
  66.             }
  67.         }
  68.     }
  69.  
  70.     private void sendOreoNotification(RemoteMessage remoteMessage){
  71.         String user = remoteMessage.getData().get("user");
  72.         String icon = remoteMessage.getData().get("icon");
  73.         String title = remoteMessage.getData().get("title");
  74.         String body = remoteMessage.getData().get("body");
  75.  
  76.         RemoteMessage.Notification notification = remoteMessage.getNotification();
  77.         int j = Integer.parseInt(user.replaceAll("[\D]",""));
  78.         Log.d("bu nedir", String.valueOf(j));
  79.         Intent intent = new Intent(this, ChatContentActivity.class);
  80.         Bundle bundle = new Bundle();
  81.         bundle.putString("id",user);
  82.         intent.putExtras(bundle);
  83.         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  84.         PendingIntent pendingIntent = PendingIntent.getActivity(this,j,intent,PendingIntent.FLAG_ONE_SHOT);
  85.         Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  86.  
  87.         OreoNotification oreoNotification = new OreoNotification(this);
  88.         Notification.Builder builder = oreoNotification.getOreoNotification(title,body,pendingIntent,defaultSound,icon);
  89.  
  90.         int i =0;
  91.         if(j>0){
  92.             Random rand = new Random();
  93.             i= rand.nextInt(100000);
  94.         }
  95.  
  96.         oreoNotification.getManager().notify(i,builder.build());
  97.     }
  98.  
  99.     private void sendNotification(RemoteMessage remoteMessage) {
  100.         String user = remoteMessage.getData().get("user");
  101.         String icon = remoteMessage.getData().get("icon");
  102.         String title = remoteMessage.getData().get("title");
  103.         String body = remoteMessage.getData().get("body");
  104.         Log.d(TAG, "sendNotification: " + remoteMessage);
  105.  
  106.         RemoteMessage.Notification notification = remoteMessage.getNotification();
  107.         int j = Integer.parseInt(user.replaceAll("[\D]",""));
  108.         Intent intent = new Intent(this, ChatContentActivity.class);
  109.         Bundle bundle = new Bundle();
  110.         bundle.putString("id",user);
  111.         intent.putExtras(bundle);
  112.         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  113.         PendingIntent pendingIntent = PendingIntent.getActivity(this,j,intent,PendingIntent.FLAG_ONE_SHOT);
  114.  
  115.         Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  116.  
  117.         NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
  118.                 .setSmallIcon(R.mipmap.ic_launcher)
  119.                 .setContentTitle(title)
  120.                 .setContentText(body)
  121.                 .setAutoCancel(true)
  122.                 .setColor(getResources().getColor(android.R.color.white))
  123.                 .setSound(defaultSound)
  124.                 .setContentIntent(pendingIntent)
  125.                 .setPriority(Notification.PRIORITY_MAX)
  126.                 .setDefaults(NotificationCompat.DEFAULT_VIBRATE);
  127.         NotificationManager noti = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  128.  
  129.         int i =0;
  130.  
  131.         if(j>0){
  132.             Random rand = new Random();
  133.             i= rand.nextInt(100000);
  134.         }
  135.  
  136.         noti.notify(i,builder.build());
  137.     }
  138. }
  139.