Code for implementing Notifications in Sketchware

This tutorial shows a Sketchware android project example in which a Notification is displayed when a Button is clicked and when the Notification is clicked, it opens a new Activity.

1. Create a new project in Sketchware. In VIEW area add an EditText edittext1, and a Button button1.

2. Using Image Manager add an images mail_white. This will be used as the Notification icon.

3. In Library manager switch on AppCompat and Design.

4. Create a new VIEW two.xml / TwoActivity.java.

5. Add a More Block: createChannel.

6. In More Block createChannel, use add source directly block and put following code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Channel name 1";
String description = "Notification channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("id 1", name, importance);
channel.setDescription(description);

NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);

}

7. In onCreate event, use the More Block createChannel.


8. Suppose you want to display content in edittext1 as notification when button1 is clicked. Then in button1 onClick event use an add source directly block and write the following code:

Intent intent = new Intent(MainActivity.this, TwoActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
android.support.v4.app.NotificationCompat.Builder builder = new android.support.v4.app.NotificationCompat.Builder(MainActivity.this, "id 1")
.setSmallIcon(R.drawable.mail_white)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentTitle("My notification")
.setContentText("The text in EditText is " + edittext1.getText().toString())
.setPriority(android.support.v4.app.NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true);

android.support.v4.app.NotificationManagerCompat notificationManager = android.support.v4.app.NotificationManagerCompat.from(MainActivity.this);
notificationManager.notify(1, builder.build());

'MainActivity.this' has to be changed to the Activity in which the code is used.

'TwoActivity.class' has to be changed to the Activity to be opened when notification is clicked.

Change contentTitle, contentText and SmallIcon as per your requirement.

9. You can also set a CustomView 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
.setContentTitle("My notification")
.setContentText("The text in EditText is " + edittext1.getText().toString())

with
.setContent(contentView)

This will display the contents of Custom View as notification.

10. Save and run the project. When button1 is clicked, it displays a notification showing contents of edittext1, and when the Notification is clicked, it opens TwoActivity.

Comments

  1. Hi there,
    I watch your videos on the youtube and i learn many tricks about sketchware, but i have a question: i need to put a web page in a string to edit it and extract some data. There many "how to" about it in the internet, but only works in the android studio. Can you help me? How to put a entire web page in a string in sketchware?

    Thank you,

    Flávio (from Brazil)
    my email: flavio23paula@gmail.com

    ReplyDelete
  2. Dude Why You Not Add To Create Own I mean Create Notiffication and Edit Text And Info And Add Progress Ba4

    ReplyDelete
  3. How can I make the project work on the main screen of the phone like app of the battery

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. How to acces data from any kind of notification.means want activity in response on notification.

    ReplyDelete
    Replies
    1. The intent part in code above let's it open MainActivity. You can change that to open any other Activity. Change it in this line: Intent notificationIntent = new Intent(getApplicationContext(),MainActivity.class);

      Delete
    2. Sir,i need to access normal text message content trough sketchware app,is it possible.

      Delete
    3. This comment has been removed by the author.

      Delete
    4. A app which is to be read a text message and retrive the part of text or whole,reacting upon the content of message.

      Delete
    5. Sanjeev, help
      Is it possible to insert a variable string to change the screen? I know it's wrong, but it would be something like this:

      Intent notificationIntent = new Intent(getApplicationContext(),(_Screen)Activity.class);

      Delete
  6. Hello please tell me how to implement notification for new message in chat ?

    ReplyDelete
    Replies
    1. Just goto OnCHildAdded event and add the above code.
      Then goto OnCustomViewBind of list view.
      Then set edittext1 as the message or username as per your requirements

      Delete
  7. Can i creat a notification from web server?

    ReplyDelete
  8. I think hes means can you create an Android widget in sketchware i myself am interested in doing so

    ReplyDelete
    Replies
    1. it's simple:
      for textview "TextView androidText = new TextView(this);"
      for edittext "EditText androidEdit = new EditText(getActivity());"
      for linear "LinearLayout androidLayout = new LinearLayout(this);"
      ...

      Delete
  9. How to make notification permanent?

    ReplyDelete
    Replies
    1. Spam the intent of the phone's CPU by running the code every millisecond. Try that

      Delete
  10. how to integrate the keypad into firebase notification so that client get the message?
    if the tutorial you share is only for applications that we install ourselves not for clients

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. How to the send. Push notifications to user .. like one signal push notifications

    ReplyDelete
  13. Failed to post on channel null

    ReplyDelete
  14. How to submit app IN sketchware competition

    ReplyDelete
  15. How to get premium sketchware for free

    ReplyDelete
  16. Now notification deprecated .
    How could I solve it

    ReplyDelete
    Replies
    1. You need to specify channel in builder . Does not generate run errors .

      Delete
  17. hai.. i love your blog so much!!!

    How do I make a webview riderect to the Play Store application when I will download an application from Google?

    Thank you for the help..

    ReplyDelete
  18. Hi. Why notifications do not work to all devices? This procedure give good results only to some phones.

    ReplyDelete
  19. how to integrate the firebase notification so that client get the message?
    bcz the tutorial you shared is only for applications that we install ourselves not for clients

    ReplyDelete
  20. create push notifications sketchware

    ReplyDelete
  21. But it's not working on Android 8 and higher...
    How to do it in new Android versions?

    P.s. sorry for long mail.

    ReplyDelete
  22. This Code for implementing Notifications don't work with Android 8. There is a New codes?

    ReplyDelete
  23. I found that this works terrific on my device, however, it does not push the notification to other devices. For example, say a chat app, when I send a message, I get notified that I sent it. None of the other 6 devices recieve notification, just the message.
    Thoughts?

    ReplyDelete
  24. Can you guys please help me about how to show listview contents in notifications when a listview item is clicked.

    ReplyDelete
  25. How to make notifications for newly added data in listview on Firebase

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. Hi ,
    I want to create a to-do list app.
    In that even if the app activity gets destroyed,i still want to give user a notification about completing task as a reminder. Is it possible through sketchware.

    (Eg. Like any alarm app , even if we close the app from RAM it still rings at certain time)

    ReplyDelete
  28. How set Snal Icon from file picker and size of the icon

    ReplyDelete
  29. Bagaimana cara membuat notifikasi untuk orang yang menjawab pertanyaan kalau dia memang orangnya yg jawab dengan benar

    ReplyDelete
  30. How can I set image picked up from file picker as icon for image

    ReplyDelete
  31. I followed your instructions but I got 5 errors android.support.v4.app.NotificationCompat cannot resolved to a type

    ReplyDelete
    Replies
    1. i have the same erros(3) "android.support.v4.app.NotificationCompat cannot resolved to a type. someone can help. i have android10

      Delete
    2. Import news that android.support.v4 has been changed to androidx.core by Google . but still after changing it I'm getting error (5)

      Delete
  32. I am created a project, and I want to run my application in background and to show notification on specific time.
    If application is not open

    ReplyDelete
  33. This comment has been removed by the author.

    ReplyDelete
  34. I have an activity called newActivity and it contains a drawer and a shared preference called time, and i have a custom view called drawer and it contains a switch now when you click the switch in the drawer i set the shared preference data key time to "ok" now what i want to do is dat in newActivity On create if shared preference time = "ok" i set the switch checked =true else i set checked to false but the problem is the switch object doesn't show when I'm in the on Create please help me how to do it, i want to save the state of the switch when you exit the app

    ReplyDelete
    Replies
    1. Use add source directly block. The id of switch will be _drawer_switch1 or something like that.

      _drawer_switch1.setChecked(true);

      Delete
  35. Is their any way to send notification directly from OneSignal Dashboard, as we send using Firebase

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Creating a Drawing View in Sketchware

Enable Fullscreen for Youtube videos in WebView

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware