Android notification example code | Nougat no deprecation

Android notification pendingintent example working in Marshmallow

Android simple notification show in notification icon. Open your application or any activity page which included in your app can be open when notification is clicked by user. Many are facing notification is not working. Here is the most simple working android notification example source code. You can send notification in any choice here in example notification is send when button is clicked. You can set system tray icon as well as notification icon notification title and small message to inform user in which purpose notification is send. Get more latest android code in NextGenEarn Attraction Android.

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
Button open = (Button) findViewById(R.id.button);
Button sendNotif = (Button) findViewById(R.id.notif);
open.setOnClickListener(new View.OnClickListener() {

@Override
publicvoidonClick(Viewview) {
Intent intent = newIntent(MainActivity.this,Settings.class);
startActivity(intent);
}
});

sendNotif.setOnClickListener(new View.OnClickListener() {
@Override
publicvoidonClick(Viewv) {
createNotification();
}
});

}
publicvoidcreateNotification() {

Intent intent = newIntent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
Notification noti = newNotification.Builder(this)

.setContentTitle("New mail from " + "attraction@new.com")
.setContentText("Subject")
.setTicker("Notification!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
}

Add a Comment

Your email address will not be published. Required fields are marked *