Skip to main content

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

Popular posts from this blog

Create Music Player app in Sketchware

To create a Music Player app in sketchware, follow the steps given below. 1. Create a new project in Sketchware. 2. In VIEW area on main.xml, add a ListView  listview1  with height wrap_content, and weight 1. Add a SeekBar  seekbar1 , an ImageView  imageview1 , and three TextViews  textview1 ,  textview2 , and  textview3 , as shown in the image below. 3. Create a CustomView  mycustom.xml  and add a TextView  textview1 , and an ImageView  imageview1  in it. For listview1 select mycustom.xml as it's customView. 4. Add a MediaPlayer component  mp , a Shared Preferences component  sp:sp  and a Timer component  timer . 5. Create six More Blocks  MPcreate(pos), MPstart , and  MPpause  for the MediaPlayer and  searchFolders ,  getFileList in [filePath]  and  extra . 6. Add three number variables  n, r  and  songPosition , and five String variables  currentfile ,  songMinutes ,  songSeconds ,  folder ,  folderName . Also add a List String  folderList,  List String  fileList  and a List Map

Admob rewarded video Ads in Sketchware

To integrate Admob Rewarded Video Ads to a Sketchware project, follow the steps given below. Prerequisites An Android project (Sketchware project) Account in Admob Always place the test ad unit ID before placing your ad unit ID. App ID and ad unit ID can be obtained by registering the app on Admob. But for using test ads no registration is required. Do not click on your own Ads. 1. Create an android project in Sketchware. 2. Add the app to your Admob account. For your app, generate an ad unit ID for Rewarded Video Ads. 3. In Sketchware project, navigate to  Library Manager  and open Admob settings. i. Click on Add manually and add the ad unit ID generated on Admob website, or use test ad unit ID ca-app-pub-3940256099942544/5224354917. ii. If you are not using any banner or interstitial ad units, select the same ad unit ID for both banner and interstitial ads. iii. Add Test Device automatically shown by Sketchware. iv. Save it. v. Switch ON Admob and AppCompat and design. 4. In this exa

Firebase auth in Sketchware for Login Activity

To create a login activity using Firebase Auth in Sketchware, follow the steps given below. This method uses Firebase authentication service for creating login. 1. In your Firebase account, go to Firebase authentication. 2. In Firebase authentication web set-up, go to SIGN-IN method, and enable Email/password and Anonymous. 3. Go to Project settings in your Firebase project and copy the Web API Key, Project ID, and App ID. 4. Paste the Project ID, App ID, and Web API Key in your project in Sketchware, in the Firebase settings. 5. On the  MainActivity  page add a File Shared preferences component  user:user  and an Intent  i . Also add a Timer  t  and a Firebase Auth  testlogin . 6. Create a new page  login.xml  with Activity called  LoginActivity . 7. In  onCreate  event of MainActivity use blocks as shown in image below. It identifies main page with  File user key page . It also checks if user is logged in to  FirebaseAuth . If user is logged in then other blocks are executed. If user