无法使Firebase适用于通知

时间:2016-06-29 22:41:09

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

下面我已经解释了我的样本,并提供了最少的代码来帮助理解这个问题。

到我的gradle文件,最后

apply plugin: 'com.google.gms.google-services'

在我的依赖项{}中添加了

 compile 'com.google.firebase:firebase-messaging:9.2.0'

我在依赖项{}

中也有以下内容
compile 'com.google.android.gms:play-services:9.2.0'
compile 'com.google.android.gms:play-services-maps:9.2.0'
compile 'com.google.android.gms:play-services-ads:9.2.0'
compile 'com.google.android.gms:play-services-auth:9.2.0'
compile 'com.google.android.gms:play-services-gcm:9.2.0'
compile 'com.google.android.gms:play-services-analytics:9.2.0'
compile 'com.google.android.gms:play-services-location:9.2.0'
compile 'com.google.maps.android:android-maps-utils:0.4.+'

我有明显的

     <!-- Google Cloud Messaging -->
        <uses-permission
            android:name="com.mcruiseon.buseeta.permission.C2D_MESSAGE"
            android:protectionLevel="signature"/>
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>

<service android:name=".support.NotificationIncoming">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <service android:name=".support.NotificationsIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

NotificationIncoming.java有

public class NotificationIncoming extends FirebaseMessagingService {


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(God.LOG_TAG, "From: " + remoteMessage.getFrom());
        Log.d(God.LOG_TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getBody());
    }

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MyBookings.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_launcher)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

在我的NotificationIDService

public class NotificationsIDService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(God.LOG_TAG, "Refreshed token: " + refreshedToken);
        sendRegistrationToServer(refreshedToken);
    }
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

在Firebase上,我完成了以下操作

  1. 创建了一个项目
  2. 添加了SHA1指纹
  3. 下载google-services.json并将其复制到app文件夹。
  4. 我从与SHA1相同的密钥库中构建了一个已签名的应用程序。并运行应用程序。然后,我从Firebase控制台发送了消息。

    没有运气。我错过了什么?我认为这是非常基本的东西。

    修改

    对于所有阅读此内容的人来说,上述内容对我来说非常有用。

1 个答案:

答案 0 :(得分:0)

没有任何更改,上面的设置/代码开始工作,我很晚才收到这些通知。

@ shadab-ansari,是的我从下拉列表中添加了应用程序。我还没有从清单中删除权限:),它现在正在工作,我没有接触它。