当前我以形式发送通知
{
"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:[声音=默认值]
E/fcmbundle:[google.message_id=0:…]
E/fcmbundle:[collapse_key=…]
E/fcmbundle:转储意图结束
这是当应用程序在后台时,我从FCM获得的数据的日志,点击通知后,它将打开启动器活动。
我已经阅读了许多与同一问题有关的答案,但对我不起作用。我正在发送数据消息,并在其中收到了这条消息。还有声音和图标。但这些都不起作用。没有通知声音,图标显示为正方形。
I have also tried the single notification in json as:
{
"to" : "...",
"notification" : {
"sound": "default",
"icon" : "myicon"
},
"data" : {}
}
但这也没有起到应有的作用。
通知是在后台发出的,但不会发出声音,也不会显示图标。
当应用程序在前台时,通知会正常工作,根据需要显示图标并用声音进行通知。
有人能帮我吗?
对于前台和后台的通知,请考虑仅使用data
有效负载。那么下面的custom MessageService class
中的override
onMessageReceived
方法对我来说是一个很好的解决方案。
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());
}
}
}
试试这个:
@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;
}
}
}