我是刚开始使用android构建应用程序,所以请原谅。
我的应用程序在后台时会收到通知,但在前台时却不会,我想同时获取两者的通知。
现在发生的是,当我在前台收到应用程序的通知时,该应用程序崩溃了。
我确实了解android在这两个事件处于两种不同状态时如何处理这两个事件,因此我必须修改onMessageReceived
来处理该数据。这是我到目前为止所拥有的
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentTitle(remoteMessage.getNotification().getTitle()).setAutoCancel(true).setContentIntent(pendingIntent)
.setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true).setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
}
侧面问题:我不确定代码的这一部分是否正确manager.notify(0, builder.build());
,据我了解,0
是某种CHANNEL_ID,因此我不确定0
是正确的值。
AndroidManifest.xml
<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=".MyFirebaseMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
这也是我的理解,为了使onMessageReceived
处理send
,我不需要使用notification
而是使用data
。所以我用php发送我的数据。
$notification = [
'title' => $title,
'body' => $message,
//'icon' =>'myIcon',
'vibrate' => 1,
'sound' => 'default'
];
$fcmNotification = [
'registration_ids' => $token,
'data' => $notification
];
json_encode($fcmNotification));
Logcat
2018-12-31 12:36:26.416 22775-22850/com.domainname.main.domainname D/msg: onMessageReceived: null
2018-12-31 12:36:26.419 22775-22850/com.domainname.main.domainname E/AndroidRuntime: FATAL EXCEPTION: Firebase-MyFirebaseMessagingService
Process: com.domainname.main.domainname, PID: 22775
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference
at com.domainname.main.domainname.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:31)
at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source)
at com.google.firebase.iid.zzc.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source)
at java.lang.Thread.run(Thread.java:762)
因此,在这一点上,我不确定问题是如何在MyFirebaseMessagingService.java
中发送数据或代码。有什么想法吗?
已解决
好吧,我弄清楚了,这主要是我发送数据的方式。我必须将我的php更改为此
$notification = [
'title' => $title,
'body' => $message,
//'icon' =>'myIcon',
'vibrate' => 1,
'sound' => 'default'
];
$extraNotificationData = [
'title' => $title,
'body' => $message,
//'icon' =>'myIcon',
'vibrate' => 1,
'sound' => 'default'
];
$fcmNotification = [
'registration_ids' => $token, //single token
'notification' => $notification,
'data' => $extraNotificationData
];
对于在发送onMessageReceived
时使用data
时不知道如何播放通知默认声音的人,可以将其添加到FirebaseMessagingService扩展名(我的名字是{{1 }})MyFirebaseMessagingService.java
答案 0 :(得分:1)
@Cesar请,您能复制并粘贴发生崩溃的日志猫部分吗?然后我们可以在那里查看确切的错误。