Reputation: 425
I am using fcm for getting push notifications in my android app. I am getting notifications well when app is opened and closed or minimised state also.But the problem is I am not getting notification sound when app in minimised or closed state.
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String refrehedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("refreshedtoken",refrehedToken+"");
PrefUtils.saveToPrefs(getApplicationContext(),PrefUtils.DEVICETOKEN,refrehedToken);
sendRegistrationToServer(refrehedToken);
}
private void sendRegistrationToServer(String refrehedToken) {
}
}
///////////////////////////////////////////////////////////////////
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getData().size()>0){
Log.d(TAG,"From:"+remoteMessage.getData()+"");
Gson gson = new Gson();
String json = gson.toJson(remoteMessage.getData());
Log.d("jsonresponce",json+",,,");
try {
JSONObject object = new JSONObject(json);
String title = object.getString("title");
String message = object.getString("message");
Log.d("firebasenotification",title+",,"+message);
sendNotificatin(title,message);
} catch (JSONException e) {
e.printStackTrace();
}
}else if(remoteMessage.getNotification()!=null){
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getTitle());
sendNotificatin(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
}
}
private void sendNotificatin(String title, String message) {
Intent intent = new Intent(this,SplashScreen.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.app_ic)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Random random = new Random();
int num = random.nextInt(99999-1000)+1000;
notificationManager.notify(num,notificationBuilder.build());
}
}
////////////////////////////////////////////////////////
<service android:name=".Fcm.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".Fcm.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/app_ic" />
Upvotes: 1
Views: 1544
Reputation: 328
I have edited my code, you forgot to add .setSound. I have also added in some other stuff that you may want in the future.
private void sendNotificatin(String title, String message) {
private void sendNotificatin(String title, String message) {
Intent intent = new Intent(this,SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_ic)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(sound) // changed from SET_DEFAULTS
.setContentIntent(pendingIntent);
/* Begin other added stuff */
.setLights(0xffff0000, 500, 250);
.setWhen(System.currentTimeMillis())
/* End other added stuff */
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Random random = new Random();
int num = random.nextInt(99999-1000)+1000;
notificationManager.notify(num,notificationBuilder.build());
}
Upvotes: 0
Reputation: 93
I haven't enough reputation to add a comment so, I am doing this for you, but I know this is inappropriate.
The reason is when app minimized, or in the background, the notification delivered to Android notification delivered to Android notification tray. There are two payloads one is "data payload" and another one is "notification payload." Just remove "notification payload" then it will work fine.
Upvotes: 3