Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.csanchez.pushexample;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v4.app.NotificationCompat;
- import android.support.v7.app.AppCompatActivity;
- import io.reactivex.functions.Consumer;
- public class MainActivity extends AppCompatActivity {
- private static final String TAG = "MainActivity";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ((PushExampleApplication) getApplication())
- .bus()
- .toObservable()
- .subscribe(new Consumer<Object>() {
- @Override
- public void accept(Object object) throws Exception {
- if (object instanceof MyNotification) {
- showNotification(((MyNotification) object).getTitle(), ((MyNotification) object).getBody());
- }
- }
- });
- }
- private void showNotification(String title, String message) {
- NotificationCompat.Builder builder =
- new NotificationCompat.Builder(this)
- .setSmallIcon(R.drawable.ic_launcher_background)
- .setContentTitle(title) //this is the title of notification
- .setColor(101)
- .setContentText(message); //this is the message showed in notification
- Intent intent = new Intent(this, MainActivity.class);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- builder.setContentIntent(contentIntent);
- // Add as notification
- NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- manager.notify(0, builder.build());
- }
- }
Add Comment
Please, Sign In to add comment