Advertisement
Guest User

Untitled

a guest
Sep 13th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. private NotificationCompat.Builder createMessageNotification(String sender, String message) {
  2.         Intent mainActivityIntent = new Intent();
  3.         mainActivityIntent.setClass(getApplicationContext(), MainActivity.class);
  4.  
  5.         PendingIntent intent = PendingIntent.
  6.                 getActivity(getApplicationContext(), 1, mainActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  7.  
  8.         return new NotificationCompat.Builder(getApplicationContext()).
  9.                 setContentTitle(sender).
  10.                 setContentText(message).
  11.                 setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).
  12.                 setContentIntent(intent).
  13.                 setAutoCancel(true).
  14.                 setSmallIcon(R.drawable.message);
  15.     }
  16.  
  17.     private Conversations.Profile findProfileById(Conversations.Root jObj, int id) {
  18.         for (Conversations.Profile profile :
  19.                 jObj.response.profiles) {
  20.             if(profile.id == id)
  21.                 return profile;
  22.         }
  23.  
  24.         // This code shouldn't be reached
  25.         return null;
  26.     }
  27.  
  28.     private void attachEventCheckLoop() {
  29.         Handler handler = new Handler();
  30.  
  31.         handler.post(new Runnable() {
  32.             @Override
  33.             public void run() {
  34.                 VK.Instance.request("messages.getConversations", new VK.ObjectResponse<Conversations.Root>() {
  35.                     @Override
  36.                     public void onSuccess(Conversations.Root jObj) {
  37.                         if(prevResponse != null) {
  38.                             for(int i = 0; i < jObj.response.items.size(); i++) {
  39.                                 Conversations.Item srcItem = prevResponse.response.items.get(i);
  40.                                 Conversations.Item newItem = jObj.response.items.get(i);
  41.  
  42.                                 if(newItem.last_message.from_id == Integer.parseInt(AppPreferences.getInstance().Data.id))
  43.                                     break;
  44.  
  45.                                 if(srcItem.last_message.id != newItem.last_message.id) {
  46.                                     String shortMessage = "";
  47.  
  48.                                     if(newItem.last_message.text == null || newItem.last_message.text.length() == 0)
  49.                                         shortMessage = "< Вложение >";
  50.                                     else
  51.                                         shortMessage = newItem.last_message.text;
  52.  
  53.                                     Conversations.Profile profile = findProfileById(jObj, newItem.conversation.peer.id);
  54.                                     NotificationCompat.Builder notify =
  55.                                             createMessageNotification(profile == null ? "Беседа" : (profile.first_name + " " + profile.last_name), shortMessage);
  56.  
  57.                                     notificationManager.notify(15, notify.build());
  58.                                 }
  59.                             }
  60.                         }
  61.  
  62.                         prevResponse = jObj;
  63.                     }
  64.  
  65.                     @Override
  66.                     public void onError(int code, String msg) {
  67.  
  68.                     }
  69.                 }, Conversations.Root.class, "&count=5&extended=1");
  70.  
  71.  
  72.  
  73.                 handler.postDelayed(this, TIME_INTERVAL * 1000);
  74.             }
  75.         });
  76.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement