在Android Firebase中,来自SNS aws的设备未收到云消息通知

时间:2017-07-21 12:10:48

标签: android amazon-web-services firebase firebase-cloud-messaging

我遇到FireBase云消息传递问题,我从设备中获取令牌并通过Google Firebase通知控制台发送通知测试,并通知移动设备。但是我没有收到AWS SNS的通知?任何人都可以帮我解决实际问题是什么?提前致谢

以下是代码。

MyFirebaseMessagingService.java

private static final String TAG = "MyFirebaseMessagingService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "FROM:" + remoteMessage.getFrom());

        //check msz contains data
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data: " + remoteMessage.getData());
        }

        //check msz contains notification

        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message body: " + remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody());
            Log.d(TAG, "From: " + remoteMessage.getData());

            Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());


        }
    }


    ////Display notification
//
    private void sendNotification(String body) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);
        //set sound for notification

        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notifiBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("NewProject")
                .setContentText(body)
                .setAutoCancel(true)
                .setSound(notificationSound)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0/*Id of notification*/, notifiBuilder.build());
    }
}

MyFirebaseInstanceIdService.java

private static final String TAG="MyFirebaseInstanceIdService";

@Override
public void onTokenRefresh() {
    //get updated token
    String refreshedtoken= FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG,"New Token:" + refreshedtoken);

清单文件如下

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.google.android.c2dm.permission.REGISTER" />
<uses-permission android:name="com.example.atg.newproject.permission.C2D_MESSAGE" />

<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=".MyFirebaseInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
    <service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>

    </service>
</application>

有人可以帮我解决问题。提前致谢

0 个答案:

没有答案