Push notification in Sketchware

 Code:

final Context context = getApplicationContext();


NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


Intent intent = new Intent(this, MainActivity.class); 

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 

androidx.core.app.NotificationCompat.Builder builder; 


    int notificationId = 1;

    String channelId = "channel-01";

    String channelName = "Channel Name";

    int importance = NotificationManager.IMPORTANCE_HIGH;


    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

        NotificationChannel mChannel = new NotificationChannel(

                channelId, channelName, importance);

        notificationManager.createNotificationChannel(mChannel);

    }

 androidx.core.app.NotificationCompat.Builder mBuilder = new androidx.core.app.NotificationCompat.Builder(context, channelId)

            .setSmallIcon(R.drawable.img2)

            .setContentTitle(_title)

            .setContentText(_message)

            .setAutoCancel(true)

            .setOngoing(false)

            .setContentIntent(pendingIntent);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

    stackBuilder.addNextIntent(intent);

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(

            0,

            PendingIntent.FLAG_UPDATE_CURRENT

    );

    mBuilder.setContentIntent(resultPendingIntent);


    notificationManager.notify(notificationId, mBuilder.build());

Comments

Popular posts from this blog

Background video in Sketchware