I'm developing a chat application and I am using Firebase Cloud Messaging. Notification works properly. When the phone is locked, the notification is received properly, but the sound is not generated.
Any help is appreciated.
FCMMessagingservice.java
package com.synergywebdesigners.nima;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.WindowManager;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import static android.app.Notification.VISIBILITY_PUBLIC;
import static com.android.volley.VolleyLog.TAG;
/**
* Created by Ashish Shahi on 13-04-2017.
*/
public class FcmMessagingService extends FirebaseMessagingService {
long[] pattern = {500,500,500,500,500,500,500,500,500};
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
PowerManager pm = (PowerManager)getSystemService(
Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK
| PowerManager.ON_AFTER_RELEASE,
TAG);
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
Intent intent = new Intent(this, Memberlist.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_action_title)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setDefaults(0)
.setLights(Color.RED, 500, 500)
.setPriority(Notification.PRIORITY_HIGH)
.setOngoing(true)
;
wl.acquire();
// ... do work...
wl.release();
/*notificationBuilder.setLights(Color.BLUE, 500, 500);
long[] pattern = {500,500,500,500,500,500,500,500,500};
notificationBuilder.setVibrate(pattern);*/
notificationBuilder.setStyle(new NotificationCompat.InboxStyle());
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Go to FCM CONSOLE and enable sound and define key 'sound'==>'default';
and go to php cansole and and type Following code 'sound'==>'default'; thats work properly
Related
Is there a way to make android notification stay on screen for over 4 seconds.
For example:
Phone App has notification for incoming call, when this notification shows up (Incoming call) it does not leave the screen until the user is interacting with the notification (Accept call / deny call / go to phone app). However I have not seen a way to make notification last on screen (foreground) for over 4 seconds. Even when i am setting the FLAG_INSISTENT and FLAG_NO_CLEAR the notification last visible on screen for 4 - 5 seconds...
FLAG_INSISTENT - keeps beeping and vibrate on high priority notification but still the notification is going off the screen after 4 seconds and the user need to drag bar down in order to "see" the notification.
I am looking for a way to put a notification the same way that the phone app is doing it meaning keep the notification ON SCREEN until the user interacts with it.
Here is an example of main activity:
My MainActivity.java:
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private NotificationManagerCompat notificationManager;
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager = NotificationManagerCompat.from(this);
Intent YesReceive = new Intent(this, com.eddieharari.notifi.Data.class);
YesReceive.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntentYes = PendingIntent.getActivity(this,12345, YesReceive,
0);
Notification notification = new NotificationCompat.Builder(this, App.CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("My Notify TITLE")
.setContentText("This is my notification Message")
.setPriority(NotificationManagerCompat.IMPORTANCE_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setOngoing(false)
.setAutoCancel(true)
.setContentIntent(pendingIntentYes)
.setPriority(Notification.PRIORITY_MAX)
.build();
notification.flags = notification.flags | Notification.FLAG_INSISTENT | Notification.FLAG_NO_CLEAR;
notificationManager.notify(1, notification);
}
}
My App.java (where i set Notification Channel)
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
public class App extends Application {
public static final String CHANNEL_1_ID = "channel1";
#Override
public void onCreate() {
super.onCreate();
createNotificationChannels();
}
private void createNotificationChannels(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel1 = new NotificationChannel(CHANNEL_1_ID, "Channel 1", NotificationManager.IMPORTANCE_HIGH);
channel1.setDescription("This is channel 1");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
}
}
}
I FOUND OUT THE FOLLOWING:
Fullscreen intent inside the notification with high priority = true (second argument for the intent) leaves the notification on top of the screen till user interaction !
When using FullScreen intents don't forget to ask for fullScreenIntent permissions within the manifest....
I'm using OneSignal Notification service to send a notification. As per documentation, I'm doing setup it worked well before for lower Android SDK. After migrating to androidX it's showing incompatible types for NotificationCompat. I didn't find a proper solution to this problem. Please comment on your answer. Find the Code below
MyNotificationExtenderService.java
package ak.wp.meto.notification;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import com.onesignal.NotificationExtenderService;
import com.onesignal.OSNotificationDisplayedResult;
import com.onesignal.OSNotificationReceivedResult;
import java.math.BigInteger;
public class MyNotificationExtenderService extends NotificationExtenderService {
#Override
protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
// Read Properties from result
OverrideSettings overrideSettings = new OverrideSettings();
overrideSettings.extender = new NotificationCompat.Extender() {
#Override
public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
// Sets the background notification color to Red on Android 5.0+ devices.
return builder.setColor(new BigInteger("FFFF0000", 16).intValue());
}
};
OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
Log.d("OneSignalExample", "Notification displayed with id: " + displayedResult.androidNotificationId);
return true;
}
}
BINGO!!! I modified my code in this way
MyNotificationExtenderService.java
package ak.wp.meto.notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.media.RingtoneManager;
import android.net.Uri;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import com.onesignal.NotificationExtenderService;
import com.onesignal.OSNotificationReceivedResult;
import ak.wp.meto.R;
import ak.wp.meto.activity.MainActivity;
import ak.wp.meto.activity.PostDetailsActivity;
import ak.wp.meto.data.constant.AppConstant;
public class MyNotificationExtenderService extends NotificationExtenderService {
#Override
protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
Uri defaultSoundUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setVibrate(new long[]{1000, 1000})
.setSound(defaultSoundUri);
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
return false;
}
}
This worked for me
First of all I would like to thank you for clicking on this thread.
Thank you so much for taking your time to read this.
I was planning for the application push a notification on a specific time.
I followed some tutorials online but it seems it doesn't work.
There is no error message on the code itself but no notification is coming out.
Here's my "Notification_receiver.java" file:
package com.example.reviewerapplication;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
public class Notification_receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context,Chapterchoice.class);
String daily10 = "daily";
intent.putExtra("daily",daily10);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Sample Title")
.setContentText("same message")
.setAutoCancel(true);
if (intent.getAction().equals("MY_NOTIFICATION_MESSAGE")) {
notificationManager.notify(100, builder.build());
}
}
}
This is what is on my MainActivity:
//Daily Notification
val calendar = Calendar.getInstance()
calendar.set(Calendar.HOUR_OF_DAY,21)
calendar.set(Calendar.MINUTE,53)
calendar.set(Calendar.SECOND,0)
val intent2 = Intent(this, Notification_receiver::class.java)
intent2.setAction("MY_NOTIFICATION_MESSAGE")
var pendingIntent = PendingIntent.getBroadcast(this,100,intent2,PendingIntent.FLAG_UPDATE_CURRENT )
var alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.timeInMillis,AlarmManager.INTERVAL_DAY,pendingIntent)
I actually got it fixed just now.
my issue is that I used "activity" in the Manifest instead of using "receiver"
I'm currently making an app on android studio and I am getting stuck.
I've mananaged to have my notifications displayed when the app is open or closed, and when the app is in foreground, I receive the notification with the modifications I made on the MyFirebaseInstanceIdService file (see the file below), but i can't manage to display the notification when the app is closed or in background (meaning when it's a data notification) with the modifications I made (like the change of icon for the notification).
here is the code of the MyFirebaseInstanceIdService, the file managing my notifications
package com.shayru.myapplication.Service;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.shayru.myapplication.R;
import java.util.Map;
import java.util.Random;
public class MyFirebaseInstanceIdService extends FirebaseMessagingService {
//Ctrl o
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if(remoteMessage.getData().isEmpty())
showNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
else
showNotification(remoteMessage.getData());
}
private void showNotification(Map<String,String> data) {
String title = data.get("title").toString();
String body = data.get("body").toString();
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID ="com.shayru.myapplication.test";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("CE Valeo");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
//R to take from "com.Shayru.myapp
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("Info");
notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
}
private void showNotification(String title, String body) {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID ="com.shayru.myapplication.test";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("CE Valeo");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
//R to take from "com.Shayru.myapp
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("Info");
notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
}
}
I put here my code...
I explain my problem..
when I create a notification, it appears to me immediately even if I enter a specific time to make it appear.
at that time instead opens automatically as if i push on notification.
can you tell me how I can play it at the time that i inserted?
thanks
import java.util.Calendar;
import java.util.StringTokenizer;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
public class Allarm {
private Context context;
public Allarm(Context context) {
this.context = context;
}
public void createAlarm(String rip,String title,String object,String dati) {
String day= "01";
String month= "01";
String year= "2014";
String hour= "00";
String minute = "02";
String date = day+"/"+month+"/"+year+" "+hour+":"+minute;
Calendar myAlarmDate = Calendar.getInstance();
myAlarmDate.setTimeInMillis(System.currentTimeMillis());
myAlarmDate.set(Integer.valueOf(year), Integer.valueOf(month),
Integer.valueOf(day), Integer.valueOf(hour),
Integer.valueOf(minute), 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent =new Intent(context, NotificationReceiverActivity.class);
intent.putExtra("dati", dati);
intent.setData(Uri.parse("content://"+date));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
context)
.setWhen(myAlarmDate.getTimeInMillis())
.setContentText(object)
.setContentTitle(title)
.setSmallIcon(R.drawable.avvio)
.setAutoCancel(true)
.setTicker(title)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
Notification notification=notificationBuilder.build();
alarmManager.set(AlarmManager.RTC_WAKEUP,
myAlarmDate.getTimeInMillis(), pendingIntent);
notificationManager.notify((int) myAlarmDate.getTimeInMillis(), notification);
}
}
The method NotificationManager.notify(int id, Notification notification) posts the notification immediately. The first argument is an id you specify and not the time the notification should be displayed.
You could use the AlarmManager to schedule a notification.