Java 应用处于前台状态时无法接收来自FCM的通知
我是Android和编程新手,我正在创建一个应用程序,该应用程序在后台接收来自FCM的消息并正常工作,因此,我添加了一个FirebaseMessagingService类,该类在onMessageReceived方法中接收通知,并跟踪google文档,但当应用程序处于前台状态时,通知仍然不起作用,我遵循了每一步,无法找出代码中的问题在哪里,因为日志对我没有帮助,我也没有收到任何错误,除了这一个,我不知道它是否是问题的根源,如果问题: 分析GCM事件java.lang.NumberFormatException中的时间戳时出错:s==null 这是我的MyFirebaseMessagingService类Java 应用处于前台状态时无法接收来自FCM的通知,java,android,node.js,firebase,firebase-cloud-messaging,Java,Android,Node.js,Firebase,Firebase Cloud Messaging,我是Android和编程新手,我正在创建一个应用程序,该应用程序在后台接收来自FCM的消息并正常工作,因此,我添加了一个FirebaseMessagingService类,该类在onMessageReceived方法中接收通知,并跟踪google文档,但当应用程序处于前台状态时,通知仍然不起作用,我遵循了每一步,无法找出代码中的问题在哪里,因为日志对我没有帮助,我也没有收到任何错误,除了这一个,我不知道它是否是问题的根源,如果问题: 分析GCM事件java.lang.NumberFormatEx
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>
我发现了问题,因为我使用的是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);
}
}
您在哪里包含了解决此问题的上述代码?感谢MyFirebaseMessagingService类—负责接收通知的类
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);
}
}