Guest User

Untitled

a guest
Feb 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. package com.example.csanchez.pushexample;
  2.  
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.support.v4.app.NotificationCompat;
  9. import android.support.v7.app.AppCompatActivity;
  10.  
  11. import io.reactivex.functions.Consumer;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14. private static final String TAG = "MainActivity";
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20.  
  21. ((PushExampleApplication) getApplication())
  22. .bus()
  23. .toObservable()
  24. .subscribe(new Consumer<Object>() {
  25. @Override
  26. public void accept(Object object) throws Exception {
  27. if (object instanceof MyNotification) {
  28. showNotification(((MyNotification) object).getTitle(), ((MyNotification) object).getBody());
  29. }
  30. }
  31. });
  32. }
  33.  
  34. private void showNotification(String title, String message) {
  35. NotificationCompat.Builder builder =
  36. new NotificationCompat.Builder(this)
  37. .setSmallIcon(R.drawable.ic_launcher_background)
  38. .setContentTitle(title) //this is the title of notification
  39. .setColor(101)
  40. .setContentText(message); //this is the message showed in notification
  41. Intent intent = new Intent(this, MainActivity.class);
  42. PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  43. builder.setContentIntent(contentIntent);
  44. // Add as notification
  45. NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  46. manager.notify(0, builder.build());
  47. }
  48. }
Add Comment
Please, Sign In to add comment