Code for implementing Notifications in Sketchware
2 minute read
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.
4. Create a new VIEW two.xml / TwoActivity.java.
5. Add a 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.
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);
}
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);
}
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.