Enable Notifications in your sketchware project


In your Sketchware project, add an image (say imageiconxyz ) which will be displayed as notification image.
Then choose the event on which you want to show notification (on button click, on image click, on Activity Create, etc.).
Suppose you want to display notification when a button is clicked. Then onButtonClick event use block
add source directly and write the following code:

Notification.Builder mBuilder = new Notification.Builder(MainActivity .this);
mBuilder.setSmallIcon(R.drawable.imageiconxyz );
mBuilder.setContentTitle("title ");
mBuilder.setContentText("text ");
mBuilder.setDefaults( Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(getApplicationContext(),MainActivity.class );
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pendingIntent).setAutoCancel(true);
notificationManager.notify(1, mBuilder.build());

The first underlined text 'MainActivity' is the name of the page on which you are using the code. Change it as per the name of the page or activity.
The second underlined text 'imageiconxyz' is name of the image which will act as notification icon. Change it as per the name of the image you have added.
The third underlined text 'title' is the title of the notification. Change it as per your requirement.
The fourth underlined text 'text' is the text shown as notification. Change it as per your requirement.
The last underlined text 'MainActivity.class' is the name of the activity or page which opens when the notification is clicked. Change it if you wish to open any other screen.
You can also set Custom View in Sketchware as notification by making little modifications to the code above. Suppose name of your Custom View is 'cview.xml'. Then add the following code just before the code provided above, to display it as notification:

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.cview );
Now in the code provided earlier, replace
mBuilder.setContentTitle("title ");
mBuilder.setContentText("text ");
with
mBuilder.setContent(contentView);

This will display the contents of Custom View as notification.

Post a Comment

0 Comments