Sending notification on post added - android

I am creating a news android app and i want to send a notification when post added to database.i know Google Firebase Console but i want to send notification automatically.i have following codes:
public void sendNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setPriority(Notification.PRIORITY_MAX);
Intent intent = new Intent(MainActivity.this, NewsActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("My Company");
mBuilder.setContentText("New news received.");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(001, mBuilder.build());
}
And using this in Apiservice:
apiService.getNews(new ApiService.OnNewsReceived() {
#Override
public void onReceived(List<News> news) {
sendNotification();
adsesAdapter = new AdsesAdapter(NewsActivity.this, ) );
recyclerView.setAdapter(adsesAdapter);
}
});
This codes just show notification when user is on app... Is there any way to show notification on news received without opening app???

Related

How to update notification title and text without sending new notification? [duplicate]

This question already has answers here:
Update text of notification, not entire notification
(3 answers)
Closed 5 years ago.
I have an app in which i have to update notification text and title without sending new notification. How do i do that i have tried below mentioned code.
code:-
notifica("Test", "Testinggggggggggg");
m_send = (Button) findViewById(R.id.send_another);
m_send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
builder.setContentTitle("Test3");
builder.setContentText("Test3333333333");
notificationManager.notify(0, builder.build());
}
});
}
public void notifica(String title, String message) {
int color = getResources().getColor(R.color.colorPrimary);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(this, HomeScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
builder = new NotificationCompat.Builder(this);
builder.setContentTitle(title);
builder.setContentText(message);
builder.setAutoCancel(false);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setColor(color);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
builder.setSound(defaultSoundUri);
builder.setContentIntent(pendingIntent);
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}
use this to update the title with same notification_id as you are using 0 for now
builder.setContentTitle(title);
notificationManager.notify(0, builder.build());
From Android Developer
notify
added in API level 1
void notify (int id, Notification notification)
Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.
Using same notification id you can update notification text using local broadcast manager.

Notification Vibration Not Working

I am trying to set vibration for firebase notification
but I think I am not doing it right
here is a code,
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this);
notification.setContentTitle("NEW NOTIFICATION");
notification.setContentText(remoteMessage.getNotification().getBody());
notification.setAutoCancel(true);
Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.icon);
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setLargeIcon(icon);
notification.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, notification.build());
notification.setVibrate(new long[] { 1000, 1000});
}
You need to configure the Notification before calling notify(). You are calling setVibrate() after calling notify(). Move your setVibrate() call to be before the notify() call.
Also note that you need to have a <uses-permission> element for the VIBRATE permission in your manifest.

Notification not showing up in android 19

I am trying to notify using notification as below: The main activity has the following code, but nothing is showing up.
mEmailSignInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("Enable Call Permission")
.setContentText("Please Enable or Accept call permission");
int NOTIFICATION_ID = 12345;
Intent targetIntent = new Intent(MainActivity.this, PermissionActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());
save();
}
});
It seems it doesnt work without an icon.
.setSmallIcon(R.drawable.small_icon);
see this post:
Notification Not showing

Firebase Error What Happened Here

When i send a notification from firebase just show notification title. it was working but now it does not work.
private void showNotification(String message) {
Intent i=new Intent(this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setAutoCancel(true);
builder.setContentTitle("Bildirim");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setTicker("selam dost.mesaj geldi");
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,builder.build());
}
}
And i am taking message like this now : "V/GoogleSignatureVerifier: com.google.android.gms signature not valid. Found" What happened to this ?

I want to show notification even if my Android app is running

I am using AWS SNS to send push notification to both SNS topics and Device Endpoints. It shows notification if the app is not running but does not show notification while the app is running.
I will like to show notification even if the app is running because the notification will inform the app user of changes happening in another section of the app so they can go check it out when they are ready. Something like getting notification of a new Whatsapp message from another group chat while you are typing message to another person.
Below is the notification builder code. Any hint will be really appreciated. Thanks.
private void displayNotification(final String message) {
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
int requestID = (int) System.currentTimeMillis();
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap large_icon = BitmapFactory.decodeResource(this.getResources(),
R.mipmap.myImage);
// Display a notification with an icon, message as content, and default sound. It also
// opens the app when the notification is clicked.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setLargeIcon(large_icon)
.setContentTitle("My Subject Title")
.setContentText(message)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
I found the problem.
#Override
public void onMessageReceived(final String from, final Bundle data) {
String message = getMessage(data);
Log.d(LOG_TAG, "From: " + from);
Log.d(LOG_TAG, "Message: " + message);
// Display a notification in the notification center if the app is in the background.
// Otherwise, send a local broadcast to the app and let the app handle it.
displayNotification(message);
/* if (isForeground(this)) {
// broadcast notification
broadcast(from, data);
} else {
displayNotification(message);
} */
}
my displayNotification() function was not called because of the "if" conditional statement that will only display notification if the application is not in foreground. So now when onMessageReceived() gets the message it passes it directly to displayNotification() even if my application is currently running, which is what I want.
Use this below code to show notification message.
private static void generateNotification(Context context, final String message)
{
try {
NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
//Define sound URI
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(context, DemoActivity.class);
notificationIntent.putExtra("message", message);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, RandomNumberGenerator.getRandom(),notificationIntent
/*new Intent(context, LuncherActivity.class)*/, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.icon_small);
mBuilder.setContentTitle("YOUR-APP-NAME");
// mBuilder.setStyle(new NotificationCompat.BigTextStyle()
// mBuilder.bigText(msg);
mBuilder.setContentText(message);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mBuilder.setSound(soundUri);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
Create a random number genetor class like below
public class RandomNumberGenerator {
public static int getRandom()
{
int random_number=0;
Random random = new Random();
random_number= random.nextInt(9999 - 1000) + 1000;
return random_number;
}
}
Try it and let me know.its working in my case.

Categories

Resources