How to Push Notifications to Android Applications from Windows

When you are scanning tons of documents with your scanner applications, probably you don’t have enough time to take a break sometimes. Because you need to stare at the screen for checking all scanning information in case of some errors. Fortunately, mobile applications can help us reduce the burden. No matter where we are, using a smartphone as the monitor is a convenient way to deal with some emergencies. In this tutorial, I’d like to share how to create an Android application for receiving notifications from a .NET scanner application which is implemented based on Dynamic .NET TWAIN.

Thinking

  1. Create socket connections between Windows and Android applications.
  2. Launch an Android background service for receiving notifications.
  3. When the service receives a message, it will notify the Android app.

Socket Connection for Message Transmission

In my previous article Wireless TWAIN Document Scanning on Android, I’ve shared how to use TCPListener for socket connection. If you are not familiar with this part, please read it.

Start and Stop Android Service

Create a class NotificationService:

public class NotificationService extends Service {

    @Override
    public void onCreate() {
    }

    @Override
    public void onDestroy() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private final IBinder mBinder = new Binder() {
        @Override
        protected boolean onTransact(int code, Parcel data, Parcel reply,
                int flags) throws RemoteException {
            return super.onTransact(code, data, reply, flags);
        }
    };
}

Declare the service tag in AndroidManifest.xml:

<service android:name="com.dynamsoft.twain.NotificationService" />

Start the service in onCreate(Bundle), and stop the service in onDestroy():

startService(new Intent(ScanAssist.this, NotificationService.class));
stopService(new Intent(ScanAssist.this, NotificationService.class));

Push Scanner Notifications to Android Apps

To send messages to Android applications, we need to invoke NotificationManager:

private NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Create an Activity IncomingMessageView for viewing incoming messages:

public class IncomingMessageView extends Activity {

    public static final String KEY_FROM = "from";
    public static final String KEY_MESSAGE = "message";
    public static final int NOTIFICATION_ID = R.layout.activity_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView view = new TextView(this);
        view.setText(getIntent().getCharSequenceExtra(KEY_FROM) + ": " + getIntent().getCharSequenceExtra(KEY_MESSAGE));

        setContentView(view);

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.cancel(NOTIFICATION_ID);
    }
}

Declare the activity tag in AndroidManifest.xml:

<activity
            android:name="com.dynamsoft.twain.IncomingMessageView"
            android:label="@string/app_name" >
</activity>

Send notifications and display messages on status bar:

        Intent notifyIntent = new Intent(this, IncomingMessageView.class);
        notifyIntent.putExtra(IncomingMessageView.KEY_FROM, from);
        notifyIntent.putExtra(IncomingMessageView.KEY_MESSAGE, message);

        PendingIntent pendingIntent =
                PendingIntent.getActivity(
                this,
                0,
                notifyIntent,
                PendingIntent.FLAG_ONE_SHOT
        );

        Notification notif = new Notification.Builder(this)
        .setContentTitle("TWAIN Scanner Status ")
        .setContentText(message)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pendingIntent)
        .setTicker(message)
        .build();

        notif.defaults = Notification.DEFAULT_ALL;

        mNM.notify(IncomingMessageView.NOTIFICATION_ID, notif);

Use Case

  1. Launch Android application and start the background service
  2. Switch the app from foreground to background
  3. Click button Scan on .NET scanner application
  4. Receive a new notification and view it in Android application.

scanassistserver_scannotificationview_notification

Source Code

https://github.com/yushulx/ScanAssist