0

I want to start a pendingIntent from a notification i send from App A = Admin to App B = users. I want to know if it possible to open a pendingIntent immediately a user or admin receives a notification(from each end)?

My App crashes when i try to open a notification sent from admin to user app and vice versa but I am using the same method below in both apps. What am i doing wrong?

NB : it is just a chat and suggestion app and it works like any other chat application.

    // sending notification to devices with versions below Oreo(android 8.0)
    private void sendNotification(RemoteMessage remoteMessage) {

        String user  = remoteMessage.getData().get("user");
        String icon = remoteMessage.getData().get("icon");
        String title = remoteMessage.getData().get("title");
        String body = remoteMessage.getData().get("body");

        RemoteMessage.Notification notification = remoteMessage.getNotification();
        // creating a pendingIntent for notification
        int j = Integer.parseInt(user.replaceAll("[\\D]",""));
        Intent intent = new Intent(this,MessageActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("userid",user);
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,j,intent,PendingIntent.FLAG_ONE_SHOT);

        //  notification sound uri
        Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        // creating an instance of Notification builder and setting its properties
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.app_logo_round)
                .setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(false)
                .setSound(defaultSound)
                .setVibrate(new long[]{1000,1000,1000})
                .setWhen(System.currentTimeMillis());
                .setContentIntent(pendingIntent);

 // creating an instance of Notification Manager
        NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        int i = 0;
        if(j > 0){
            i = j;
        }

        // starting the notification
        nm.notify(i,builder.build());

 }

What I want to achieve is to be able to open app from notification if an admin sends a message which comes with a notification to the user and vice versa.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ali sampson
  • 321
  • 4
  • 7
  • `"My App crashes"` - whats the stacktrace then (at least top 3-4 frames)? – pskink Jan 05 '19 at 13:31
  • Below is the error am getting from the stack trace : Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() at com.google.firebase.database.DatabaseReference.child(com.google.firebase:firebase-database@@16.0.5:101) at io.icode.concaregh.application.chatApp.MessageActivity.onCreate(MessageActivity.java:152) at android.app.Activity.performCreate(Activity.java:6375) – ali sampson Jan 05 '19 at 13:49
  • Thanks guys but i just deviced another way of doing it just by opening the activity before the MessageActivity just like how whatsApp does when u click on a notification. It works now and i believe this is cool. – ali sampson Jan 05 '19 at 14:13
  • The `NullPointerException` comes from a call to `DatabaseReference.child()`, which is not in the code you shared. When asking questions about an error in code, always share the [minimal, complete code that produces that error](http://stackoverflow.com/help/mcve). In the case of a `NullPointerException`, I highly recommend reading [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) to learn how to troubleshoot it. – Frank van Puffelen Jan 05 '19 at 14:33
  • Alright thanks man – ali sampson Jan 07 '19 at 08:50

0 Answers0