gpt4 book ai didi

java - Firebase 云消息传递 - onMessageReceived 不工作

转载 作者:搜寻专家 更新时间:2023-10-30 19:50:12 27 4
gpt4 key购买 nike

我想在我的应用中添加一个简单的通知功能。我关注了this YouTube 视频和这两个 Firebase 文档 URL 12加上 Android Studio 中的 Firebase 工具助手(它表示我已连接到 Firebase)。出于某种原因,在我的旧应用程序(代码发布在下面)上执行这些步骤和文档后,它不允许我接收通知。但是,如果我在一个全新的应用程序上执行相同的步骤,它就会完美运行。我在后台、 Activity 和终止状态下在同一物理设备和环境中测试了这两个应用程序。每次我创建的新演示应用程序都没有问题,但我想要通知的旧应用程序不起作用。两者均在未获取设备 ID 的情况下进行了测试。我什至没有收到任何错误日志或任何 TAG 日志。我认为我编译的项目之一有干扰,不确定到底是什么,但我可能必须去那里看看。

我还检查了所有这些 SO 帖子:1 2 3等等

附言。我在下面删除了我的包名称,我在 FireBase 上检查了多次,它们匹配,所以我知道这不是问题所在。但是,我的新演示应用程序的 FireBase 显示我的应用程序已连接,但我的旧应用程序的 FireBase 没有。另外,我之前设置了赏金,但仍然遇到同样的问题。

这是我有问题的代码:

Notification.java(它是文档要求的服务)

public class Notification extends FirebaseMessagingService {
public Notification() {
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
showNotification(remoteMessage.getData().get("message"));

Log.d("FMC", "Message Notification Body: " + remoteMessage.getNotification().getBody());
}

private void showNotification(String message) {
Intent i=new Intent(this, SplashScreen.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent=PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("FCM TITLE").setContentText(message)
.setSmallIcon(R.drawable.pt_icon)
.setDefaults(android.app.Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent);

NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,builder.build());
}
}

Notice I do not even get a log in the notification.java above.

Project.gradle

buildscript {
repositories {
jcenter()
maven {
url 'https://maven.google.com/'
name 'Google'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.gms:google-services:3.2.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()

maven { url "https://jitpack.io" }
maven { url 'https://maven.google.com' }
google()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

app.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 26

defaultConfig {

minSdkVersion 16
targetSdkVersion 26
versionCode 16
versionName "2.6"
}
dexOptions {
jumboMode = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//Code below is added to fix random error ("Fix the issues identified by lint")
lintOptions {
abortOnError false
}
productFlavors {
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
//compile project(path: ':backend', configuration: 'android-endpoints')
//Removed the 0.2.+
compile 'com.daprlabs.aaron:cardstack:0.3.1-beta0'
//noinspection GradleCompatible
compile 'com.android.support:appcompat-v7:25.4.0'
compile "com.android.support:appcompat-v7:19.0.+"
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'

compile 'com.github.danylovolokh:video-player-manager:0.2.0'
compile 'com.github.danylovolokh:list-visibility-utils:0.2.0'



implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.google.firebase:firebase-messaging:11.8.0'
compile 'com.google.firebase:firebase-core:11.8.0'


compile 'com.orhanobut:dialogplus:1.11@aar'


compile 'com.nineoldandroids:library:2.4.0'
compile files('libs/sinch-android-rtc-3.9.14.jar')
compile 'com.amazonaws:aws-android-sdk-s3:2.4.4'
compile 'com.github.chrisbanes:PhotoView:1.2.6'
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.5.0'
compile 'com.github.amlcurran.showcaseview:library:5.4.3'
compile 'com.github.d-max:spots-dialog:0.7@aar'
compile 'com.victor:lib:1.0.4'
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:recyclerview-v7:23.0.0'
compile 'me.grantland:autofittextview:0.2.0'
compile 'com.wang.avi:library:1.0.5'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.braintreepayments.api:drop-in:2.3.8'
compile 'com.braintreepayments.api:braintree:2.3.9'
compile 'com.loopj.android:android-async-http:1.4.9'
compile 'com.getbase:floatingactionbutton:1.10.1'
compile 'com.mxn.soul:flowingdrawer-core:1.2.2'
compile 'com.github.rengwuxian:MaterialEditText:2.1.4'
compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
compile 'net.gotev:uploadservice:3.2.5'
compile 'in.srain.cube:ultra-ptr:1.0.11'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha9'
testCompile 'junit:junit:4.12'
//implementation 'com.android.support:appcompat-v4:23.+'
}


apply plugin: 'com.google.gms.google-services'

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-feature
android:name="android.hardware.microphone"
android:required="false" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<!--
so the app can be found on tablets Google Play

<uses-permission android:name="android.permission.CALL_PHONE" />
-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<!-- <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/> -->
<!-- <uses-permission android:name="android.permission.WAKE_LOCK" /> -->
<!-- <uses-permission android:name="android.permission.READ_PHONE_STATE" /> -->

<grant-uri-permission
android:path="string"
android:pathPattern="string"
android:pathPrefix="string" />

<application
android:allowBackup="true"
android:icon="@mipmap/_icon"
android:label=""
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
tools:node="replace">
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name=".Notification">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

<!-- &lt;!&ndash; -->
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. -->
<!-- See README() for more. -->
<!-- &ndash;&gt; -->
<!-- <meta-data -->
<!-- android:name="com.google.firebase.messaging.default_notification_icon" -->
<!-- android:resource="@drawable/pt_icon" /> -->
<!-- &lt;!&ndash; -->
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming -->
<!-- notification message. See README() for more. -->
<!-- &ndash;&gt; -->
<!-- <meta-data -->
<!-- android:name="com.google.firebase.messaging.default_notification_color" -->
<!-- android:resource="@color/colorAccent" /> -->

<activity android:name=".SignInForm" />
<activity android:name=".SignUpPage" />
<activity android:name=".SellerFillOutForm" />
<activity android:name=".BuyerFillOutForm" />
<activity
android:name=".BuyerHomePage"
android:label="@string/title_activity_buyer_home_page" />

<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="http" />

<activity android:name=".VideoPlayerActivity" />
<activity android:name=".BackgroundTask" />

<service
android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
android:enabled="true" />

<activity android:name=".EnterCreditCard" />
<activity android:name=".PickMeeting" />
<activity android:name=".HowBillingWorks" />
<activity android:name=".ReBillingPlan" />
<activity android:name=".PayBill" />
<activity
android:name=".Buyer_Home_Page"
android:configChanges="locale|orientation" />
<activity android:name=".Seller_Home_Page" />
<activity android:name=".ProductList" />
<activity android:name=".EditProduct" />
<activity android:name=".EditSellerAccount" />
<activity android:name=".EditBuyerAccount" />
<activity android:name=".Video_Calling.incomingVideoCall" />
<activity android:name=".Video_Calling.CallScreenActivity" />
<activity android:name=".SellerAnalytics" />
<activity android:name=".Swipe_Layout.SwipeLayout" />
<activity android:name=".Swipe_Layout.PopUpActivity" />
</application>

</manifest>

Working code below (the app I made just to test my steps and works 100%):

Notifaction.java

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import static android.content.ContentValues.TAG;

public class Notifaction extends FirebaseMessagingService {
public Notifaction() {
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
showNotification(remoteMessage.getData().get("message"));
}

private void showNotification(String message) {
Intent i=new Intent(this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent=PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("FCM TITLE").setContentText(message)
.setSmallIcon(R.drawable.ic_launcher_background)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent);

NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,builder.build());
}
}

AndroidManiest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".Notifaction">
<intent-filter>
<action
android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

</application>

</manifest>

app.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
defaultConfig {
applicationId ""
minSdkVersion 14
targetSdkVersion 26
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:appcompat-v7:26.1.0'
implementation 'com.google.firebase:firebase-messaging:11.8.0'
compile 'com.google.firebase:firebase-core:11.8.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}


apply plugin: 'com.google.gms.google-services'

项目.grdle

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.gms:google-services:3.2.0'

// NOTE: Do not place your application dependencies here; they
belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

此外,我还向两个项目添加了 .json 文件。这是一个截图: link .

最佳答案

如果您正在接收设备 token 并且您有服务器 api key ,请使用 pushtry 检查您的代码是否正常工作.如果您在代码中犯了任何错误,它会给出类似无效注册404 错误 等错误。对于 FCM 的实现,您可以按照本教程- FCM Tutorial

我还注意到在您的 build.gradle 文件中,defaultConfig 中没有任何 applicationId "Package Name"。我不确定,但这是包名称,当我们在 firebase 控制台上创建项目时使用(带包的项目名称)

关于java - Firebase 云消息传递 - onMessageReceived 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48968525/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com