Code To Create Notification in Sketchware App






To implement notification in sketchware, follow these steps

1. In your Sketchware project, add an image (say IMG) which will be displayed as notification icon.


2. If you want to implement notification on button click then onButtonClick event use block add source directly and put the following code






Notification.Builder mBuilder = new Notification.Builder(MainActivity.this);
//Mainactivity.this is your activity
mBuilder.setSmallIcon(R.drawable.img);
//IMG is your image
mBuilder.setContentTitle("title");
//"title" will be your title
mBuilder.setContentText("text");
//"text" will be your subtitle
mBuilder.setDefaults( Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(getApplicationContext(),MainActivity.class);
//Main activity.class will be your class
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pendingIntent).setAutoCancel(true);
notificationManager.notify(1, mBuilder.build());


If found any error please comment down

8 Comments

  1. WARNING in istorage/emulated O sketchware/mysc/604/app/src/main/java/com/ bezoul/notepad/MainActivity java (at line 76) Notification.Builder mBuilder new Notification Builder(MainActivity this) The constructor Notification.Builder(Context) is deprecated 2. ERROR in/storage/emulated/0/ sketchware/mysc/604/app/src/main/java/com/ bezoui/motepad/MainActivity java (at line 80) mBuilder.setSmallicon(R drawable,img) img cannot be resolved or is not a field 3. WARNING in/storage/emulated/O/sketchware/mysc/604/app/src/mainvjava/com/ bezoul/notepad/MainActivity.java (at line 90) mBuilder.setDefaults( Notification DEFALULT SOUND Notification DEFAULT VIBRATE) The method setDefaults(int) from the type Notification.Builder is deprecated 3 problems (1 error, 2 warnings)

    ReplyDelete
    Replies
    1. You need to specify channel id
      Notification.Builder mBuilder = new Notification.Builder(MainActivity.this,"channel_id");

      Delete
    2. I have added the channel Id, and no error during complie, but even I press the button, no notification is shown at all, please help, tks.

      Delete
  2. Delete " " near with ";"

    ReplyDelete
    Replies
    1. I don't understand what you mean. I don't get the notifications at all but there was no compilation problem .

      Delete
  3. Hi, I have a question: It doesn't work if I close the app ?

    ReplyDelete
  4. it works but it only shows on single device; the device which used to send the notification, but other devices doesn't show the notification, any fix? thanks.

    ReplyDelete
Previous Post Next Post