我已经在我的应用程序中集成了FCM。每当应用程序在后台运行时,都不会收到fcm消息。我已经尝试了通知类型和数据类型的消息。甚至通知消息也不会显示在通知托盘中。他们只是迷路了!
请帮我解决我要去的地方。我已经按照文档按照所有内容进行了研究,并且整整一周都在进行研究。
我的清单:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application ....>
<service
android:name=".MyFirebaseMessagingService"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
我的Firebase消息服务:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
SharedPreferences sharedPref;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("message"));
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
}
@Override
public void onNewToken(String token) {
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
//Sending handled here
}
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String title, String messageBody) {
Intent intent = new Intent(this, UserHomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("title", title);
intent.putExtra("message", messageBody);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 273, intent,
PendingIntent.FLAG_ONE_SHOT);
//String channelId = getString(R.string.default_notification_channel_id);
String channelId = "Sandeep123";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setContentTitle(title)
.setSmallIcon(R.mipmap.ic_launcher_foreground_new)
.setColorized(true)
.setColor(Color.BLUE)
.setContentText(messageBody)
.setAutoCancel(true)
.setVisibility(VISIBILITY_PUBLIC)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
getString(R.string.channel_name),
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(createID(), notificationBuilder.build());
}
public int createID() {
Date now = new Date();
int id = Integer.parseInt(new SimpleDateFormat("ddHHmmss", Locale.US).format(now));
return id;
}
}
我已经在firebase控制台中添加了我的调试和SHA-1版本。我不知道我还能在哪里出错。当应用程序处于活动状态时,它可在所有设备上使用。但是当应用程序在后台运行时,它根本不起作用。
* ------------更新-服务器端代码
function sendGcmNotification($amountAdded, $tok,$des){
define( 'API_ACCESS_KEY', '***' );
$title = "Rs.".$amountAdded." added as credit";
$notificationMsg = "***";
//$token = array();
//$token[] = $tok;
$msg =
[
'message' => $notificationMsg,
'title' => $title
];
$android = ["priority"=>"high"];
$fields =
[
'to' => $tok,
'data' => $msg,
'time_to_live' => 900,
'priority' => 10,
'android' => $android
];
$headers =
[
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
}
Log cat日志如下:
2019-01-24 11:14:08.310 1541-1578/? W/ActivityManager: Background start not allowed: service Intent { act=com.google.firebase.MESSAGING_EVENT pkg=in.dailydelivery.dailydelivery cmp=in.dailydelivery.dailydelivery/.MyFirebaseMessagingService (has extras) } to in.dailydelivery.dailydelivery/.MyFirebaseMessagingService from pid=26445 uid=10210 pkg=in.dailydelivery.dailydelivery 2019-01-24 11:14:08.311 26445-26445/?
E/FirebaseInstanceId: Error while delivering the message: ServiceIntent not found
请帮帮我。 桑迪普。
答案 0 :(得分:0)
为使FCM在前台/后台将消息传递到应用以在通知栏上创建通知,从应用服务器发送的消息格式应与以下格式匹配,请注意通知消息不会收到应用在后台运行时的任何回调,只会收到数据消息
通知消息格式:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
数据消息:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
有关更多详细信息,请参见下面的链接 https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message
为使fcm在设备锁定或处于后台状态时发送推送通知,来自应用服务器的消息应具有以下标记
{
....
"android": {"priority":"high"},
"priority": 10,
....
}
有关更多详细信息,请参见下面 https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message
答案 1 :(得分:0)
在后台接收时带有通知和数据有效负载的消息。在这种情况下,通知将传递到设备的系统托盘,数据有效载荷将在启动器活动的意图之外传递。
答案 2 :(得分:-1)