[英]Cant receive Notifications from FCM while the app is in Foreground state
我是 Android 和編程新手,我正在創建一個應用程序,該應用程序從 FCM 接收消息並且它在后台運行時正常工作,所以我添加了一個 FirebaseMessagingService 類,該類在 onMessageReceived 方法中接收通知並遵循谷歌文檔,但仍然當應用程序處於前台狀態時,通知不起作用,我已經按照每一步操作,但無法找出代碼中的問題所在,因為日志對我沒有幫助,而且我沒有收到任何錯誤,除了這個錯誤我不知道它是否是問題的來源:在 GCM 事件中解析時間戳時出錯 java.lang.NumberFormatException: s == null
這是我的 MyFirebaseMessagingService 類
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import com.app.muhammadgamal.swapy.R;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String click_Action = remoteMessage.getNotification().getClickAction();
String messageTitle = remoteMessage.getNotification().getTitle();
String messageBody = remoteMessage.getNotification().getBody();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
.setSmallIcon(R.drawable.ic_main_logo)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
Intent intent = new Intent(click_Action);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
int mNotificationID = (int)System.currentTimeMillis();
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.notify(mNotificationID,mBuilder.build());
}
}
和我的清單文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.muhammadgamal.swapy">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_main_logo"
android:label="@string/app_name"
android:roundIcon="@drawable/ic_main_logo"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".Activities.SignInActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activities.SignUpActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Activities.ProfileActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Activities.NavDrawerActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Activities.SwapCreationActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".Activities.ReceivedSwapRequest">
<intent-filter>
<action android:name="com.app.muhammadgamal.swapy.Activities.ReceivedSwapRequest_NOTIFICATION_TASK"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
<service
android:name=".Notifications.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
和我的索引功能
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/Notifications/{receiver_user_id}/{notification_id}')
.onWrite((data, context) =>
{
const receiver_user_id = context.params.receiver_user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification to send to :' , receiver_user_id);
if (!data.after.val())
{
console.log('A notification has been deleted :' , notification_id);
return null;
}
const DeviceToken = admin.database().ref(`/Users/${receiver_user_id}/device_token`).once('value');
return DeviceToken.then(result =>
{
const token_id = result.val();
const payload =
{
notification:
{
title: "New Swap Request",
body: `you have a new Swap Request, Please Check.`,
click_action: "com.app.muhammadgamal.swapy.Activities.ReceivedSwapRequest_NOTIFICATION_TASK"
}
};
return admin.messaging().sendToDevice(token_id, payload)
.then(response =>
{
return console.log('This was a notification feature.');
});
});
});
我發現了問題,這是我使用的是 Android O 並且沒有為通知設置通道 ID,如果您想在 Android 8.0 或更高版本上接收它,這是必須的
所以這是解決方案代碼
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.