我正在尝试设置推送通知系统,为此,我正在将Firebase Cloud Messaging与Pusher一起使用。为了实现它,我遵循了this tutorial。
设置FCM时,我能够从FCM的仪表板发送通知。 然后,当我设置Pusher时,收到通知后,我的应用程序崩溃。
我已经检查了互联网上的多个帖子,但是找不到能解决我的问题的帖子。我已经多次检查了我的gradle文件中的依存关系。
这是我的MainActivity.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 {
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)
答案 0 :(得分:1)
我发现了同样的问题。我改回com.google.firebase:firebase-messaging:17.5.0
。问题解决了。