Search code examples
androidbroadcastreceiverandroid-notificationsandroid-pendingintent

How do i give boolean status success or not to pendingIntent in broadcast Receiver


i'm developing addAction notification from iBeacon, how do i give boolean status success or not to this actionIntent from this pendingIntent notification which will received in onReceive method in ActionReceiver.class this is the notification method from MainActivity.class

Intent broadcastIntent = new Intent(this, ActionReceiver.class);
        broadcastIntent.putExtra("action", "notif1");
        PendingIntent actionIntent = PendingIntent.getBroadcast(
                this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.th_notif_logo)
                .setTicker("Your Title")
                .setWhen(System.currentTimeMillis())
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .addAction(R.drawable.ic_petunjuk_icon,"Clue", actionIntent)
                .setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
                .setStyle(new Notification.BigTextStyle().bigText(message))
                .setContentIntent(pIntentAction)
                .setPriority(Notification.PRIORITY_HIGH)
                .build();

this is the onReceive method in ActionReceiver.class

@Override
    public void onReceive(Context context, Intent intent) {
        String action=intent.getStringExtra("action");
        if(action.equals("notif1")){
            SharedPreferences preferences = context.getSharedPreferences("MYPREFS", MODE_PRIVATE);
            final String nama_tim = preferences.getString("username", "Key not correct");
            InserData(nama_tim, "fsrd");
            Toast.makeText(context.getApplicationContext(),"Action Receiver berhasil masuk",Toast.LENGTH_SHORT).show();
        }
        else if(action.equals("action2")){

        }
        //This is used to close the notification tray
        Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        context.sendBroadcast(it);
    }

so when i got boolean status success or not, i can do something in MainActivity.class

    if (actionIntent == success){
      //do something
    }

really appreciate your help guys..


Solution

  • On your MainActivity.class

    Intent broadcastIntent = new Intent("action-key"); // Specify the action to your intent.
    broadcastIntent.setPackage(getPackageName());
    
    // The values you want receive in the BroadcastReceiver.
    broadcastIntent.putExtra("key-action", "notif1");
    broadcastIntent.putExtra("key-boolean", true); 
    
    PendingIntent actionIntent = PendingIntent.getBroadcast(
                        this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT) 
    NotificationCompat.Action notificationAction = new NotificationCompat.Action(R.drawable.ic_action, "Action Name", actionIntent)
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);
    
    Notification notification = new NotificationCompat.Builder(this,"channelId")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("Your Title")
                .setWhen(System.currentTimeMillis())
                .setContentTitle("test")
                .setContentText("test")
                .setAutoCancel(true)
                .addAction(notificationAction) // Adds an action button to the notification
                .setContentIntent(contentIntent) // Launches the intent when you click the notification. 
                .setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
                .setPriority(Notification.PRIORITY_HIGH)
                .build();
    ....
    
    ....
    // Instead of using your own class receiver, you have to use the default default class receiver if you want your data to reach to your MainActivity.class easily.
    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction() == "action") {
                    bool result = intent.getBooleanExtra("key-boolean", false); // Your value
                }
            }
        };
    
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("action");
    registerReceiver(broadcastReceiver, intentFilter);