簡體   English   中英

未收到GCM推送通知

[英]GCM Push Notification not received

我嘗試將此代碼用於GCM推送通知。 在這個發送通知中,我使用this 但是它顯示的消息很酷! 消息發送成功,正在檢查您的設備...但是我的設備未收到通知。

嘗試使用Firebase,這里是Push-Notification https://firebase.google.com/docs/cloud-messaging/的文檔,或者您可以查看本教程https://www.simplifiedcoding.net/android-push-notification-tutorial -使用-火力/

您應該使用FCM。 看到

1)將此行添加到build.gradle:
dependencies { compile 'com.google.firebase:firebase-messaging:9.8.0'}

2)將此服務添加到清單。
<service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service>
3)將此類添加到您的項目中。 我建議您保存您的FCM令牌:

public class MyInstanceIDListenerService extends FirebaseInstanceIdService {


@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d("FirebaseService", "Refreshed token: " + refreshedToken);

    SharedPreferences sharedPref = getSharedPreferences("YOUR_SETTING_NAME", Context.MODE_PRIVATE);
    //There are optional steps
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("token", refreshedToken);
    //notify the token update 
    editor.putBoolean("tokenUpdate",true);
    editor.commit();
    //You can send this token to your server (if you have)
    sendServer(refreshedToken)
    }
}

4)現在,將您的應用程序注冊到https://console.firebase.google.com/ 這將生成一個JSON文件,您必須將其放入應用程序文件夾中。

5)對於生成FCM消息,您有2種可能性:
5.1)使用默認的Firebase控制台: https ://console.firebase.google.com/project/fantamanager-2017/notification/compose
5.2)構建您的服務器應用程序(如果需要,我可以將您的Php服務器代碼發送給您)

6)將此服務添加到您的Manifest.xml

<service android:name="FcmBroadcastReceiver">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
</service>

並創建一個可以攔截推送的類:

 public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final ID_NOTIFICATION = ...
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

       String from,data;
       from=remoteMessage.getFrom());


        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
           data=remoteMessage.getNotification().getBody());
        }

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
        sendNotification(data);
    }



    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

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

或者,如果您有自定義服務,請使用以下方法更改onMessageReceived函數的主體:

   Map data = message.getData();
    if (data.containsKey(YOUR_DATA_FIELD_1)) {
        String field1= data.get(YOUR_DATA_FIELD_1).toString();
        String field2= data.get(YOUR_DATA_FIELD_2).toString();
        ....
        sendNotification(field1,field2...);
        return;
    }

您是否嘗試了部分或全部代碼? 因為我最后看到Coz,因此gcm的新應用程序已關閉。 僅FCM可用。 因此,您需要按照他們所說的進行注冊,在應用中實現json文件,然后繼續。 如果您已經有一個實現gcm的項目,並且嘗試了此代碼的一部分,請檢查密鑰是否正確,並且尚未從該項目或其他項目復制它們的密鑰。 或清單文件中的某些元數據丟失。

暫無
暫無

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

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