我仔细查看了所有可以找到的答案,即使在前台,特定的代码功能也无法正常工作。我尝试更改清单,更改代码,在记录器中得到的只是以下两点: D / FA:记录事件(FE):notification_receive(_nr),...
这是我的清单文件:
using System.Linq;
[HttpGet("/questions")]
public ActionResult<List<QuestionDetails>> GetQuestions()
{
return _quizService.GetAllQuestions()
.Select(x => new QuestionDetails
{
Id = x.Id,
Question = x.QuestionQuiz,
ImageName = x.ImageName,
Options = new [] { x.Option1, x.Option2, x.Option3, x.Option4 }
.Where(option => !string.IsNullOrEmpty(option))
})
.OrderBy(y => Guid.NewGuid())
.Take(10)
.ToList();
}
这是通知服务:
<?xml version="1.0" encoding="utf-8"?>
这是应用gradle文件
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/avatar"
android:label="@string/app_name"
android:roundIcon="@drawable/avatar"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".StartActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"/>
<service android:name=".GettingDeviceTokenService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".NotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
和GettingDeviceTokenService:
public class NotificationService extends FirebaseMessagingService {
public static int NOTIFICATION_ID = 1;
private static final String CHANNEL_ID = "1";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("+++", remoteMessage.toString());
if (remoteMessage.getData().size() > 0) {
Log.d("dataa", "Data Payload: " + remoteMessage.getData().toString());
}
}
}
编辑:通过更改
中的GettingDeviceTokenService的意图过滤器解决了该问题。apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.xghos.Wrenchy"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'net.hockeyapp.android:HockeySDK:5.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-messaging:17.3.0'
implementation 'com.ethanhua:skeleton:1.1.1'
implementation 'io.supercharge:shimmerlayout:2.1.0'
implementation "com.daimajia.swipelayout:library:1.2.0@aar"
}
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '26.1.0'
}
}
}
}
apply plugin: 'com.google.gms.google-services'
到
package com.example.xghos.Wrenchy;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class GettingDeviceTokenService extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("DeviceToken ==> ", s);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
答案 0 :(得分:1)
您应该考虑数据消息和显示消息之间的区别。
显示消息的有效载荷带有键notification
,当应用程序在后台时会自动显示,如果应用程序已在前台,则也会调用onMessageReceived()
。
数据消息具有有效载荷密钥data
。他们总是调用onMessageReceived()
答案 1 :(得分:1)
您已在清单中注册了NotificationService
服务。自从最新更新Firebase通知以来,同一服务将处理新令牌和收到的消息。在您的情况下,您应该仅在清单GettingDeviceTokenService
中注册,因为您同时覆盖了这两种方法并删除了NotificationService
。
答案 2 :(得分:0)
尝试这种方式。完全适合我。 1.创建服务
public class YourService extends FirebaseMessagingService {
public static int NOTIFICATION_ID = 1;
@Override
public void onNewToken(String s) {
super.onNewToken(s);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this, "2")
.setSmallIcon(R.drawable.your_icon)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (NOTIFICATION_ID > 1073741824) {
NOTIFICATION_ID = 0;
}
Objects.requireNonNull(notificationManager).notify(NOTIFICATION_ID++, mNotifyBuilder.build());
}
}
现在将其添加到清单
<service
android:name=".YourService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>