创建点击发送通知事件
Button send=findViewById(R.id.send_notification);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification=new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground))
.build();
manager.notify(1,notification);
}
});
点击发送通知事件,点击通知跳转到应用中
Button send=findViewById(R.id.send_notification);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击通知 触发事件
Intent intent =new Intent(MainActivity.this,NotificationLayoutActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,0);
//发送通知
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification=new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")))
.build();
manager.notify(1,notification);
}
});