目前我发送的通知为:
{
"data" : {
"sound": "default",
"icon": "ic_app_launcher",
"..." : "..."
.
.
.
}
}
E / fcmbundle:倾倒意图开始
E / fcmbundle:[google.sent_time = ...]
E / fcmbundle:[from = ...]
E / fcmbundle:[icon = ic_app_launcher]
E / fcmbundle:[sound = default]
E / fcmbundle:[google.message_id = 0:...]
E / fcmbundle:[collapse_key = ...]
E / fcmbundle:倾倒意图结束
这是Log当我在后台使用应用时从FCM获取的数据,点击通知后会打开启动器Activity。
我已经阅读了许多与同一问题相关的答案,但对我不起作用。我正在发送数据消息,并且内部已收到。声音和图标也。但是不行。没有通知声音,图标显示为正方形。
I have also tried the single notification in json as:
{
"to" : "...",
"notification" : {
"sound": "default",
"icon" : "myicon"
},
"data" : {}
}
但这也不会起作用。
通知来自后台,但没有发出声音,也没有显示图标。
当应用程序处于前台时,通知会正常显示图标,并根据需要通知声音。
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
对于前台和后台的通知,请考虑仅使用data
有效负载。然后您的override
中的onMessageReceived
custom MessageService class
方法下面是一个对我来说很好的解决方案。
notificationPayload = {"data": { "title": "your title" ,"body": "Your body"}}
public class CustomFirebaseMessagingService extends FirebaseMessagingService {
final String CHANNEL_ID = "YOUR_CHANNEL_ID";//when targeting android-o
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().size() > 0) {
sendNotification(remoteMessage);
}
}
private void sendNotification(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String title = data.get("title");
String message = data.get("body");
Intent resultIntent = new Intent(this, YOUR_TARGET_ACTIVITY.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_ONE_SHOT
);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true);
int notificationId = (int) System.currentTimeMillis();
mBuilder.setSound(alarmSound);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (manager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
"Human Readable channel id",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setShowBadge(true);
manager.createNotificationChannel(channel);
}
manager.notify(notificationId, mBuilder.build());
}
}
}
答案 1 :(得分:0)
试试这个:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage != null && remoteMessage.getData().size() > 0) {
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
sendPushNotification(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
sendPushNotification方法
private void sendPushNotification(JSONObject json) {
try {
//getting the json data
JSONObject data = json.getJSONObject("data");
//creating MyNotificationManager object
MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
//creating an intent for the notification
mNotificationManager.showSmallNotification(title, message, intent);
OR
mNotificationManager.showBigNotification(title, message, imageUrl, intent);
} catch (JSONException e) {
Log.e(TAG, "Json Exception: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
MyNotificationManager类:
public class MyNotificationManager {
public int ID_BIG_NOTIFICATION = 234;
public int ID_SMALL_NOTIFICATION = 235;
private Context mCtx;
public MyNotificationManager(Context mCtx) {
this.mCtx = mCtx;
ID_BIG_NOTIFICATION = getRandom();
ID_SMALL_NOTIFICATION = getRandom();
}
private int getRandom() {
Random random = new Random();
return random.nextInt(99999) + 1;
}
//the method will show a big notification with an image
//parameters are title for message title, message for message text, url of the big image and an intent that will open
//when you will tap on the notification
public void showBigNotification(String title, String message, String url, Intent intent) {
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mCtx,
ID_BIG_NOTIFICATION,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(getBitmapFromURL(url));
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
Notification notification;
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setStyle(bigPictureStyle)
.setDefaults(Notification.DEFAULT_VIBRATE|Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS)
.setSmallIcon(R.drawable.ic_app_icon)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.ic_app_icon))
.setContentText(message)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID_BIG_NOTIFICATION, notification);
}
//the method will show a small notification
//parameters are title for message title, message for message text and an intent that will open
//when you will tap on the notification
public void showSmallNotification(String title, String message, Intent intent) {
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mCtx,
ID_SMALL_NOTIFICATION,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
Notification notification;
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setSmallIcon(R.drawable.ic_app_icon)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.ic_app_icon))
.setContentText(message)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID_SMALL_NOTIFICATION, notification);
}
//The method will return Bitmap from an image URL
private Bitmap getBitmapFromURL(String strURL) {
try {
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}