首先,我不是专业人士。我还是Android开发的新手所以请原谅我的新朋友。
我正在使用Quickblox平台构建聊天应用。我使用FCM(Firebase云消息传递)平台在用户之间传递通知。在quickblox文档中,他们说我只需要在AndroidManifest.xml中注册注册服务器和监听器,我就可以收到通知了。
这就是我所做的,我能够在logcat中收到通知
D/QBASDK: QBFcmPushListenerService: data: {user_id=Yacoov, message=Hello World}
D/QBASDK: QBFcmPushListenerService: From: 640804138209
但问题是立即收到我得到Null Point Exception的内容
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.william.harusem, PID: 5163
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.BroadcastReceiver.onReceive(android.content.Context, android.content.Intent)' on a null object reference
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:311)
at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:47)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:120)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
ListenerService.java:
public class MyNotification extends QBFcmPushListenerService {
private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
RemoteMessage.Notification notification = remoteMessage.getNotification();
Map<String, String> data = remoteMessage.getData();
String userId = data.get("user_id");
String message = data.get("message");
displayNotifcation(userId, message);
}
private void displayNotifcation(String title, String contentText) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setVibrate(new long[]{0, 100, 100, 100, 100, 100})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(contentText);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
的AndroidManifest.xml
<meta-data
android:name="com.quickblox.messages.TYPE"
android:value="FCM" />
<meta-data
android:name="com.quickblox.messages.SENDER_ID"
android:value="@string/sender_id" />
<meta-data
android:name="com.quickblox.messages.QB_ENVIRONMENT"
android:value="PRODUCTION" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<service android:name=".MyNotification">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="com.quickblox.messages.services.fcm.QBFcmPushInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
P.S。我有完全相同的代码在其他项目上运行。我在这个项目中使用了Fragments和ViewPager,但在另一个项目中我使用了Activity。我不知道它是否相关。
非常感谢帮助。