Send the data to one app to another app using intent and broadcast - Part 1

Hi Friends, Have a great day. It's my 75th blog. I'm very slow, but I'm post only necessary and important posts only, hope so.

This time we'll look some manual broadcast message sending from another application, best way to for testing the notifications.

By Using send broadcast.
In First Application
ComponentName componentName = new ComponentName("com.sample.app", "com.sample.app.receiver.NotificationReceiver");
Intent intent = new Intent();
intent.setComponent(componentName);
String message = "Order ID:  #AER46798 ";
intent.putExtra("EXTRA_TITLE", "New Order Received");
intent.putExtra("EXTRA_MESSAGE", message);
sendBroadcast(intent);

In Second Application Receiver
public class NotificationReceiver extends BroadcastReceiver {

    public static String TAG = "PCM";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "message_received");
        String title = "";
        String message = "";
title = intent.getStringExtra("EXTRA_TITLE");
        message = intent.getStringExtra("EXTRA_MESSAGE");

        System.out.println(message);

      
        if (!message.equals("")) {
            // to open the activity
            Intent myInt = new Intent(context, DrawerActivity.class);
            myInt.putExtra("notification", true);
            PendingIntent pendingIntent = PendingIntent.getActivity(
                    context,
                    0,
                    myInt,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteViews customView = new RemoteViews(context.getPackageName(), R.layout.layout_notification);
            customView.setImageViewResource(R.id.mImage, R.drawable.icon_app);
            customView.setTextViewText(R.id.mTitle, title);
            customView.setTextViewText(R.id.mMessage, message);

            final int randomId = generateRandom();
            Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.icon_app);

            NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
            bigTextStyle.bigText(message);
            bigTextStyle.setBigContentTitle(title);

            Notification directReplyNotification = new NotificationCompat.Builder(context)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setLargeIcon(largeIcon)
                    .setSmallIcon(R.drawable.alert_bell)
                    .setStyle(bigTextStyle)
                    .setColor(ContextCompat.getColor(context.getApplicationContext(), R.color.colorPrimary))
                    .setLights(ContextCompat.getColor(
                            context.getApplicationContext(), R.color.colorPrimary), 1000, 1000)
                    .setVibrate(new long[]{800, 800, 800, 800})
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .setPriority(NotificationManager.IMPORTANCE_HIGH)
                    .build();


            final NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(randomId, directReplyNotification);
            Handler handler = new Handler(Looper.getMainLooper());
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    notificationManager.cancel(randomId);
                }
            }, 15000);

        }
    }

    public int generateRandom() {
        Random random = new Random();
        return random.nextInt(9999 - 1000) + 1000;
    }
}

Don't forget to register the receiver in AndroidManifest in Second Application:

<receiver android:name="com.sample.app.receiver.NotificationReceiver"
            android:enabled="true"
            android:exported="true">
</receiver>

Comments

Popular posts from this blog

Flutter Bloc - Clean Architecture

What's new in android 14?

Dependencies vs Dev Dependencies in Flutter