且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android - Firebase通知推送通知不起作用

更新时间:2022-12-06 14:25:42

不要忘记,我有同样的问题,它是通过定义启用,导出为true在我的服务,试图做我喜欢的,..

 < service android:name =。MyFirebaseMessagingService
android:enabled =true
android:exported =true>
< intent-filter>
< action android:name =com.google.firebase.MESSAGING_EVENT/>
< / intent-filter>
< / service>
< / aplication>

然后设置权限

 < uses-permission android:name =android.permission.INTERNET/> 
< uses-permission android:name =com.google.android.c2dm.permission.RECEIVE/>
< uses-permission android:name =android.permission.WAKE_LOCK/>


UPDATE

Unfortunately, I didn't solve this issue and what I did is create a new project. Fortunately my new project works. One thing I noticed from my newly created project, when I am sending notification messages, some messages didn't arrive to my device. So I think its my Internet connection problem (my idea only).

I am trying to implement basic receiving of Push Notifications using Firebase. I based this tutorial:

https://www.codementor.io/android/tutorial/send-push-notifications-to-android-with-firebase

My Problem is I don't receive any messages at all. I have sent more than 5 messages using the Firebase Console and still nothing happens.

Here is my implementation of FirebaseInstanceIdService:

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

public static final String debugTag = "MyFirebaseIIDService";

@Override
public void onTokenRefresh() {
    super.onTokenRefresh();

    //Getting registration token
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();

    //Displaying token in logcat
    Log.e(debugTag, "Refreshed token: " + refreshedToken);

}

private void sendRegistrationToServer(String token) {
    //You can implement this method to store the token on your server
    //Not required for current project
}

}

My FirebaseMessagingService implementation:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public static final String debugTag = "MyFirebaseApp";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Log.d(debugTag, "Notification Received!");

    //Calling method to generate notification
    sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());

}

//This method is only generating push notification
private void sendNotification(String title, String messageBody) {
    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);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

}

My Manifest file:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".activities.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".messagingservice.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <service android:name=".tokenservice.MyFirebaseInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

</application>

What am I doing wrong? Or what am I lacking to implement?

I am really needing some help with this one. Any help will be greatly appreciated. Thank you very much!

and dont forget, I had the same problem and it is solved by defining enabled, exported to true in my service, trying to do like me,..

  <service android:name=".MyFirebaseMessagingService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
</aplication>

and then set permission

<uses-permission android:name="android.permission.INTERNET" />            
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>        
<uses-permission android:name="android.permission.WAKE_LOCK" />