Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private NotificationCompat.Builder createMessageNotification(String sender, String message) {
- Intent mainActivityIntent = new Intent();
- mainActivityIntent.setClass(getApplicationContext(), MainActivity.class);
- PendingIntent intent = PendingIntent.
- getActivity(getApplicationContext(), 1, mainActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- return new NotificationCompat.Builder(getApplicationContext()).
- setContentTitle(sender).
- setContentText(message).
- setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).
- setContentIntent(intent).
- setAutoCancel(true).
- setSmallIcon(R.drawable.message);
- }
- private Conversations.Profile findProfileById(Conversations.Root jObj, int id) {
- for (Conversations.Profile profile :
- jObj.response.profiles) {
- if(profile.id == id)
- return profile;
- }
- // This code shouldn't be reached
- return null;
- }
- private void attachEventCheckLoop() {
- Handler handler = new Handler();
- handler.post(new Runnable() {
- @Override
- public void run() {
- VK.Instance.request("messages.getConversations", new VK.ObjectResponse<Conversations.Root>() {
- @Override
- public void onSuccess(Conversations.Root jObj) {
- if(prevResponse != null) {
- for(int i = 0; i < jObj.response.items.size(); i++) {
- Conversations.Item srcItem = prevResponse.response.items.get(i);
- Conversations.Item newItem = jObj.response.items.get(i);
- if(newItem.last_message.from_id == Integer.parseInt(AppPreferences.getInstance().Data.id))
- break;
- if(srcItem.last_message.id != newItem.last_message.id) {
- String shortMessage = "";
- if(newItem.last_message.text == null || newItem.last_message.text.length() == 0)
- shortMessage = "< Вложение >";
- else
- shortMessage = newItem.last_message.text;
- Conversations.Profile profile = findProfileById(jObj, newItem.conversation.peer.id);
- NotificationCompat.Builder notify =
- createMessageNotification(profile == null ? "Беседа" : (profile.first_name + " " + profile.last_name), shortMessage);
- notificationManager.notify(15, notify.build());
- }
- }
- }
- prevResponse = jObj;
- }
- @Override
- public void onError(int code, String msg) {
- }
- }, Conversations.Root.class, "&count=5&extended=1");
- handler.postDelayed(this, TIME_INTERVAL * 1000);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement