For am push notification using FCM. Problem with some problems in VIVO V3 phones. The same code works on other devices (Blackberry, Huawei, Samsung) with all the below scenarios.
I tested 3 scenarios;
- When the application is in the foreground.
- When the application is in the background.
- When the application completely kills.
The first condition 2 works well. Only a problem with the third. When the application kills, I do not receive a notification.
The code below uses the server side to send a data notification message,
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"title" : "Notification Title",
"body" : "Notification Message",
},
}
The following code, which is used in my application,
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import me.leolin.shortcutbadger.ShortcutBadger;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "debug";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData()!=null)
Log.d(TAG, "Notification Message get data: " + remoteMessage.getData().toString());
if (remoteMessage.getNotification()!=null)
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
try {
JSONObject jsonObject = new JSONObject(remoteMessage.getData());
Log.d(TAG , "Json DATA Notification::" + jsonObject.toString());
String message = jsonObject.getString("title");
String messageContent = jsonObject.getString("body");
sendNotification(message, messageContent);
}catch (JSONException e) {
e.printStackTrace();
Log.d("debug" , "Exception");
}
}
private void sendNotification(String messageTitle, String messageBody) {
Intent intent = new Intent(this, MainActivity.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_launcher)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , notificationBuilder.build());
}
@Override
public void onCreate() {
super.onCreate();
}
}
Blackberry, Huawei, Samsung. . . .