Android Notification Java Example
Set the notification content
To get started, you need to set the notification's content and channel using a NotificationCompat.Builder object. The following example shows how to create a notification with the following:
- A small icon, set by
setSmallIcon(). This is the only user-visible content that's required. - A title, set by
setContentTitle(). - The body text, set by
setContentText(). - The notification priority, set by
setPriority(). The priority determines how intrusive the notification should be on Android 7.1 and lower. (For Android 8.0 and higher, you must instead set the channel importance—shown in the next section.)
Note: In Android 10 (API level 29) and higher, the platform automatically generates notification action buttons if an app does not provide its own. If you don't want your app's notifications to display any suggested replies or actions, you can opt-out of system-generated replies and actions by using
setAllowGeneratedReplies() and setAllowSystemGeneratedContextualActions().Notification importance
The possible importance levels are the following:
- Urgent: Makes a sound and appears as a heads-up notification.
- High: Makes a sound.
- Medium: No sound.
- Low: No sound and does not appear in the status bar.
XML Layout for NotificationActivity :activity_notify.xml to display message here
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/notifyText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textSize="22sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
IN UR JAVE FILE MainActivity.java:Define any where on click or After server response From Database:
// politics politics createNotification( getResources().getString(R.string.channel_pass), getResources().getString(R.string.pass_content), getResources().getString(R.string.channel_pass), NotificationCompat.PRIORITY_HIGH, 100 );IN STRING FILE DEFINE:
<string name="channel_Pass">Pass</string>
<string name="Pass_content">Pass Number generated</string>
NOW elaborate above method in same java file as:
Notice that the NotificationCompat.Builder constructor requires that you provide a channel ID. This is required for compatibility with Android 8.0 (API level 26) and higher, but is ignored by older versions./*** Create Notification* Param* 1. title* 2. content* 3. channelId* 4.priorty* 5. notificationId*/privatevoidcreateNotification(String title, String content,String channedId,intpriorty,intnotificationID) {Intent intent =newIntent(this, NotifyActivity.class);intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);Bundle extras =newBundle();extras.putString(NotifyActivity.notify_title, title);extras.putString(NotifyActivity.notify_content, content);intent.putExtras(extras);intent.setAction(Intent.ACTION_VIEW);PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), notificationID, intent,PendingIntent.FLAG_UPDATE_CURRENT);NotificationCompat.Builder notificationBuilder =newNotificationCompat.Builder(getApplicationContext(), channedId).setSmallIcon(R.drawable.ic_notifications_active).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)).setAutoCancel(true).setLights(Color.BLUE,500,500).setVibrate(newlong[]{500,500,500}).setPriority(priorty).setContentTitle(title).setContentText(content).setContentIntent(pendingIntent).setVisibility(NotificationCompat.VISIBILITY_PUBLIC);// Since android Oreo notification channel is needed.NotificationManagerCompat notificationManager = NotificationManagerCompat.from(MainActivity.this);// Since android Oreo notification channel is needed.if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotificationChannel channel =newNotificationChannel(channedId,channedId,NotificationManager.IMPORTANCE_HIGH);channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);notificationManager.createNotificationChannel(channel);}Notification notification = notificationBuilder.build();notificationManager.notify(notificationID, notification);playNotificationSound();}publicvoidplayNotificationSound() {try{Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);Ringtone r = RingtoneManager.getRingtone(MainActivity.this, defaultSoundUri);r.play();}catch(Exception e) {e.printStackTrace();}}}Create a channel and set the importance
Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of
NotificationChanneltocreateNotificationChannel(). So the following code is blocked by a condition on theSDK_INTversion:NotifyActivityimportandroid.os.Bundle;importandroid.os.PersistableBundle;importandroid.widget.TextView;importandroid.widget.Toast;importandroidx.annotation.Nullable;importandroidx.appcompat.app.AppCompatActivity;publicclassNotifyActivityextendsAppCompatActivity {publicstaticString notify_title ="notify_title";publicstaticString notify_content ="notify_content";privateTextView textView;@OverrideprotectedvoidonCreate(@NullableBundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_notify);textView = findViewById(R.id.notifyText);updateUI();}privatevoidupdateUI() {String notifyData =getIntent().getExtras().get(notify_title) +" \n"+getIntent().getExtras().get(notify_content);textView.setText(notifyData);}}

Post a Comment
0Comments