I'm developing a notification method. it's working successfully. but how can I implement Onclick method to navigate a specific activity?
private void notification() {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
{
NotificationChannel channel=new NotificationChannel("n","n", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager=getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder=new NotificationCompat.Builder(this,"n")
.setContentText("Alert")
.setSmallIcon(R.drawable.ic_notifications)
.setAutoCancel(true)
.setContentText("Check your fit Room Request");
NotificationManagerCompat managerCompat=NotificationManagerCompat.from(this);
managerCompat.notify(999,builder.build());
}
Intent intent = new Intent(this, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Then
NotificationCompat.Builder builder=new NotificationCompat.Builder(this,"n")
.setContentIntent(pendingIntent)
.....
Related
I'm trying to start an activity by clicking on a notification but that only works when I'm already in the app.
Here's the code I think has to do with it:
private void sendNotification(String body) {
Intent intent = new Intent(ImpFirebaseMessagingService.this, codeActivity.class);
intent.putExtra("body", body);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Implementation TFA")
.setContentText(body)
.setAutoCancel(true)
.setSound(notificationSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
The following works in my app:
private void sendReminderNotification(Context context) {
Resources resources = context.getResources();
Intent notificationIntent = new Intent(context, TitleActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.my_diary_launcher_icon))
.setContentTitle("MyDiary")
.setContentText("Reminder to input entry.")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());
}
Use pendingIntent to pass data of intent:
private void sendReminderNotification(Context context) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Implementation TFA")
.setContentText(body)
.setAutoCancel(true)
.setSound(notificationSound);
Intent intent = new Intent(ImpFirebaseMessagingService.this, codeActivity.class);
intent.putExtra("body", body);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
public class MyBroadcaseciver extends BroadcastReceiver {
MediaPlayer mymedia;
#Override
public void onReceive(Context context, Intent intent) {
mymedia=MediaPlayer.create(context,R.raw.alarm);
mymedia.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
}
}
The code above is my broadcast receiver, which plays a song when it fires. This is working as expected, however I wish to call a push notification to pop here and also my notification isn't working here.
Notification.Builder bulider = new Notification.Builder(this)
.setContentTitle("Rainfall Alert")
.setContentText("Todays Rain");
Notification notification = bulider.build();
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0,notification);
Help is appreciated.
public void sendNotification(String title, String body, Intent intent,int pushid) {
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(body))
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri);
if(intent!=null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
notificationBuilder.setContentIntent(pendingIntent);
}
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
call this funcation in onrecive method
Intent intent = new Intent(context, Mainscreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
sendNotification("title","message", intent, 0);
public class MyBroadcaseciver extends BroadcastReceiver {
MediaPlayer mymedia;
#Override
public void onReceive(Context context, Intent intent) {
mymedia=MediaPlayer.create(context,R.raw.alarm);
mymedia.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, Mainscreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
sendNotification("title","message", intent, 0);
}
public void sendNotification(String title, String body, Intent intent,int pushid) {
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(body))
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri);
if(intent!=null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
notificationBuilder.setContentIntent(pendingIntent);
}
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Well, I'm trying to use notifications, but this code doesn't work. I've tested it on 4.4 and 5.0. I can't understand, what is wrong.
public void onClick(View view) {
Context context = getApplicationContext();
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Resources res = context.getResources();
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(contentIntent)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Title")
.setContentText("Text");
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFY_ID, notification);
}
I'll be grateful for answer.
It is probably because Google changes its notification API quite a lot in each Android Edition. So the API you used not compatible for multi Android version. But Google release appcompat-v7 \appcompat-v4 to solve this stuff.
Try the following code:
public void send(View v) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Context context = getApplicationContext();
Intent notificationIntent = new Intent(context, TestNotificationActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Notification notification = builder
.setContentIntent(contentIntent)
.setContentTitle("this is title")
.setContentText("this is content")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
manager.notify(NOTIFY_ID, notification);
}
Remember to import android.support.v7.app.NotificationCompat.
I am trying to write a simple notification code in a button that create a notification when i press on it but the notification doesn't appear when i press on the button here is the code i am using
int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(SessionsActivity.this, MainActivity.class);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(SessionsActivity.this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(SessionsActivity.this)
.setContentTitle("hii")
.setContentText("hii2")
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =NotificationManagerCompat.from(SessionsActivity.this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
Intent viewIntent = new Intent(SessionsActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, viewIntent , PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.app_name))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
notificationBuilder.setStyle(inboxStyle);
inboxStyle.setBigContentTitle("sdjshfjnds");
inboxStyle.addLine("sdjjsdfn");
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int NOTIFICATION_ID = 100;
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
I tried to create a notification that starts a activity by clicking on it and which you can't swipe away.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_FROM_BACKGROUND);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 1234567890;
mNotificationManager.notify(mId, mBuilder.build());
with the code above the notification gets displayed, but nothing happens when I click on it and you can swipe it away.
To keep the Notification, in that way the user can't click it away in any way, add this flag
Notification mNotification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(mId, notification);
To start an Activity you have to use this flag for your Intent
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Do like this:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");
Intent intent = new Intent(YourActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = mBuilder.build();
noti.flags |= mBuilder.build().FLAG_NO_CLEAR | mBuilder.build().FLAG_ONGOING_EVENT;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, noti);
You want a foreground service as detailed here
It's worth noting that only android 4.3 added the persistent notification you seem to be after and it can be overriden by the users settings.
try using like this
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_UPDATE_CURRENT);
in MainActivity.java
Dont not consume your notification; ie instead of calling below 2 line code in oncreate() call under onDestro()
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(mId);
You can use NotificationCompat.Builder's .setOngoing(true), use it like this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setContentTitle("Downloading Something")
.setContentText("Downloading")
.setSound(null)
.setDefaults(0)
.setOngoing(true);
// Makes it so the notification is clickable but not destroyable by swiping-away or hitting "clear all"