我没有收到来自Firebase的推送通知

时间:2019-01-26 20:32:30

标签: android firebase push-notification firebase-cloud-messaging

我启用了Firebase Messaging Service。一切都做对了,但是最后通知不在手机上。我尝试了该建议,并在其他类似问题中发布了帮助,但没有帮助。

MessagingService.java

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    showNotification(remoteMessage.getNotification().getBody());

}

public void showNotification(String message){
    PendingIntent pi = PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0);
    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("**")
            .setContentText(message)
            .setAutoCancel(true)
            .build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0,notification);
}
}

清单

<?xml version="1.0" encoding="utf-8"?>

<!-- android:roundIcon="@mipmap/ic_launcher_round" -->

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

<application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:usesCleartextTraffic="true"
    android:hardwareAccelerated="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name="*****">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name="******.MessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/icon" />

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />

</application>

通过不使用通知构建器的方式。这有关系吗?请帮我弄清楚。

0 个答案:

没有答案