簡體   English   中英

當應用程序處於前台狀態時接收通知-(第28頁)

[英]Receiving notification while app is in foreground - (pie 28)

我是第一次使用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
];

對於那些在發送data時使用onMessageReceived時不知道如何播放通知默認聲音的用戶,可以將其添加到FirebaseMessagingService擴展(我的名字是MyFirebaseMessagingService.java.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))

@Cesar請,您能復制並粘貼發生崩潰的日志貓部分嗎? 然后,我們可以在那里查看確切的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM