我正在尝试设置一个推送通知系统,为此,我将Firebase Cloud Messaging 与Pusher一起使用。我遵循了本教程,以便实现它。
设置 FCM时,我能够从 FCM 的控制面板发送通知。然后,当我设置 Pusher 时,在收到通知时,我的应用程序崩溃了。
我已经检查了互联网上的多个帖子,但我找不到任何解决我问题的帖子。我已经多次检查了我的 gradle 文件中的依赖项。
这是我的主要活动.java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.pusher.pushnotifications.PushNotifications;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PushNotifications.start(getApplicationContext(), "9e3bfbb7-a6b0-4bcf-aa74-47b4b04fa530");
PushNotifications.addDeviceInterest("hello");
}
}
build.gradle (Module:app( - Dependencies
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-messaging:17.6.0'
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-auth:16.2.1'
implementation 'com.pusher:push-notifications-android:1.4.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'
}
为了发送通知,我正在使用 Pusher 网站提供的 curl 命令:
curl -H "Content-Type: application/json" -H "Authorization: Bearer 99E1F894C23ECD14DE98003053B3AFC1D3A4335C97C0D1DF5E1105C719979CA3" -X POST "https://9e3bfbb7-a6b0-4bcf-aa74-47b4b04fa530.pushnotifications.pusher.com/publish_api/v1/instances/9e3bfbb7-a6b0-4bcf-aa74-47b4b04fa530/publishes" -d '{"interests":["hello"],"fcm":{"notification":{"title":"Hello","body":"Hello, world!"}}}'
我的应用程序检测到通知,但在收到通知时崩溃,出现错误:
I/FCMMessageReceiver: Got a valid pusher message.
E/AndroidRuntime: FATAL EXCEPTION: Firebase-WrappedFirebaseMessagingService
Process: com.example.fcmapp, PID: 10924
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:142)
at com.google.firebase.messaging.zzb.<init>(Unknown Source:5)
at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source:58)
at com.google.firebase.iid.zzb.run(Unknown Source: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:764)
我发现了同样的问题。我改回com.google.firebase:firebase-messaging:17.5.0
.问题解决了。