[英]Android App crash when is trying to receive a push notification trough firebase
UPDATES:更新:
Apparently, the crash was solved with "throws NullPointerException" on the onMessageReceived method, but the notification does not reach the app.显然,崩溃是通过 onMessageReceived 方法上的“throws NullPointerException”解决的,但通知没有到达应用程序。
Android Studio suggests that methods .getTitle()
and .notify()
in MyFirebaseMessagingService.java could generate NullPointerException, but I don't know why. Android Studio 建议.getTitle()
和.notify()
方法可能会生成 NullPointerException,但我不知道为什么。
Hello Everyone,大家好,
I'm trying to send push notifications to my app trough the firebase cloud messaging service.我正在尝试通过 firebase 云消息服务向我的应用程序发送推送通知。
There are two cases.有两种情况。
Here's the logcat:这是logcat:
2019-12-12 22:47:00.367 5727-5881/com.maunexus D/FA: Logging event (FE): notification_receive(_nr), Bundle[{ga_event_origin(_o)=fcm, ga_screen_class(_sc)=MainActivity, ga_screen_id(_si)=375841024272355326, message_device_time(_ndt)=0, message_type(_nmc)=display, message_name(_nmn)=Yeeey!, message_time(_nmt)=1576183623, message_id(_nmid)=4180683689345961897}]
2019-12-12 22:47:00.367 5727-5878/com.maunexus E/AndroidRuntime: FATAL EXCEPTION: Firebase-MyFirebaseMessagingService
Process: com.maunexus, PID: 5727
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.messaging.RemoteMessage$Notification com.google.firebase.messaging.RemoteMessage.getNotification()' on a null object reference
at com.maunexus.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:21)
at com.google.firebase.messaging.FirebaseMessagingService.zzc(com.google.firebase:firebase-messaging@@20.1.0:78)
at com.google.firebase.messaging.zze.run(com.google.firebase:firebase-messaging@@20.1.0:2)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source:6)
at java.lang.Thread.run(Thread.java:919)
2019-12-12 22:47:00.417 5727-5727/com.maunexus I/FIAM.Headless: Removing display event listener
2019-12-12 22:47:00.434 5727-5878/com.maunexus I/Process: Sending signal. PID: 5727 SIG: 9
MainActivity.java主活动.java
package com.maunexus;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
public class MainActivity extends AppCompatActivity {
private WebView webView;
Activity activity;
private static final String TAG = "MainActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
webView = (WebView) findViewById(R.id.webviewid);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
}
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
webView.getSettings().setSupportMultipleWindows(false);
webView.getSettings().setSupportZoom(false);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://pari365.mg");
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
//To do//
return;
}
// Get the Instance ID token//
String token = task.getResult().getToken();
String msg = getString(R.string.fcm_token, token);
Log.d(TAG, msg);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.super_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_back:
onBackPressed();
break;
case R.id.menu_forward:
onForwardPressed();
break;
case R.id.menu_refresh:
webView.reload();
Toast.makeText(this, "Reloading... Please Wait!", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
private void onForwardPressed() {
if (webView.canGoForward()) {
webView.goForward();
} else {
Toast.makeText(this, "Already there! ;)", Toast.LENGTH_SHORT).show();
}
}
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
}
}
}
MyFirebaseMessagingService.java MyFirebaseMessagingService.java
package com.maunexus;
import android.app.NotificationManager;
import android.content.Context;
import android.media.RingtoneManager;
import androidx.core.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
RemoteMessage remoteMessage;
@Override
public void onMessageReceived(RemoteMessage message) {
super.onMessageReceived(remoteMessage);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setStyle(new NotificationCompat.BigTextStyle())
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
BuildGrandle App BuildGrandle 应用程序
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.maunexus"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-core:17.2.1'
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation 'com.google.firebase:firebase-messaging:20.1.0'
implementation 'com.google.firebase:firebase-inappmessaging-display:19.0.2'
implementation 'com.android.support:multidex:1.0.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
apply plugin: 'com.google.gms.google-services'
You have a null reference error in "onMessageReceived".您在“onMessageReceived”中有一个空引用错误。
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.messaging.RemoteMessage$Notification com.google.firebase.messaging.**RemoteMessage.getNotification()' on a null object reference**
at com.maunexus.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:21)
You're calling specific properties of that message which try to pull data from a null value:您正在调用该消息的特定属性,这些属性尝试从空值中提取数据:
.setContentTitle(remoteMessage.getNotification().getTitle())
Whatever you're passing in as the remoteMessage variable appears to be null.无论您作为 remoteMessage 变量传入的任何内容似乎都为空。 You should start there and see what you're passing in.你应该从那里开始,看看你传递了什么。
The error message is telling you that message
is null.错误消息告诉您message
为空。 It's not clear to me why FCM is doing that.我不清楚为什么 FCM 这样做。 It looks like a bug in FCM to me.对我来说,它看起来像是 FCM 中的一个错误。 For now, you should check that message
is not null before calling getNotification()
on it in order to prevent your app from crashing.现在,您应该在调用getNotification()
之前检查该message
是否为空,以防止您的应用程序崩溃。
I strongly suggest filing a bug report with Firebase support and explain clearly how to reproduce the issue.我强烈建议向 Firebase 支持提交错误报告,并清楚地解释如何重现该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.