我正在从GCM向FCM迁移通知。在GCM中,当应用程序处于前台,后台或被杀时,我能够收到通知,但是在FCM中,当浏览器未打开/杀死应用程序时,我无法收到通知。我错过了什么????
的AndroidManifest.xml
<service
android:name=".Services.ExtendedInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".Services.FcmMessageHandler"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
FCMMessageHandler.java
公共类FcmMessageHandler扩展了FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage message) {
// Check if message contains a data payload.
if (message.getData().size() > 0) {
Log.d("ABC", "Message data payload: " + message.getData());
}
sendNotification("test");
}
private void sendNotification(String message) {
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setColor(getResources().getColor(R.color.button_background))
//.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
.setContentTitle("App")
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_MAX)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationNumber /* ID of notification */, notification);
}
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.ic_notification : R.drawable.ic_launcher;
}
}
json请求正文:
{
"notification":{
"title":"Title",
"message":"Message.",
"icon" = "ic_notification",
"sound" = "default",
"click_action":"FCM_PLUGIN_ACTIVITY"
},
"data":{
"title":"Title",
"message":"Message.",
"icon" = "ic_notification",
"sound" = "default"
},
"to":"chIFk6ljlfdskfdsifosdifnodsifnsdofindsofindsfoindsifvndsnmdkslakjdn2p",
"priority":"high"
}
答案 0 :(得分:0)
hr{border-color: #000000 !important; border-top:none !important; height:1px !important;}
试试这个。我是这样做的。使用NotificationUtils。下面是NotificationUtils代码
private void showNotificationMessage(Context context, String title, String message, Intent intent) {
notificationUtils = new NotificationUtils(context);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("message", message);
//intent.putExtra("notification", "noti");
notificationUtils.showNotificationMessage(title, message, intent);
}
/**
* Showing notification with text and image
*/
private void showNotificationMessageWithBigImage(Context context, String title, String message, Intent intent, String imageUrl) {
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("message", message);
//intent.putExtra("notification", "noti");
notificationUtils.showNotificationMessage(title, message, intent, imageUrl);
}
使用isAppIsInBackground方法代码。它可以帮助你
答案 1 :(得分:0)
我让你用按摩标签替换身体标签。更改服务器中的有效负载。
{
"to" : "deviceToken",
"notification" : {
"body" : "Pass body here",
"title" : "Title For Notification",
"icon" : " icon ",
"sound" : "notification sound here"
}
}