我正在编写一个应用程序,并且想对其执行推送通知,以通知用户发生了什么事。我环顾四周,找到了所需的代码,但不确定如何解决我面临的问题。可能是因为代码已过期或不正确。这是我在说的:
public class FirebaseInstanceIDService extends FirebaseInstanceIdService implements Constants
public class FireBaseMessagingService extends FirebaseMessagingService implements Constants
删除工具常量会导致其损坏,并可以在需要调用的清单中使用。
我尝试删除该工具,但是会导致另一个错误,如果我删除了该工具而只留下了“ class”,它就不会被公开,但我无法调用该应用的清单
ID服务
import android.util.Log;
import android.view.View;
import com.google.android.gms.common.internal.Constants;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService implements Constants {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
}
}
会议服务
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.common.internal.Constants;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
public class FireBaseMessagingService extends FirebaseMessagingService implements Constants {
private static final String TAG = "MyFirebaseMsgService";
private static int count = 0;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.d(TAG, "Notification Message TITLE: " + remoteMessage.getNotification().getTitle());
Log.d(TAG, "Notification Message BODY: " + remoteMessage.getNotification().getBody());
Log.d(TAG, "Notification Message DATA: " + remoteMessage.getData().toString());
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getTitle(),
remoteMessage.getNotification().getBody(), remoteMessage.getData());
}
//This method is only generating push notification
private void sendNotification(String messageTitle, String messageBody, Map<String, String> row) {
PendingIntent contentIntent = null;
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(contentIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(count, notificationBuilder.build());
count++;
}
}
这是我要使用的完整代码
我希望它能够将其用于推送通知。但是,如果出现错误,将无法使用。
答案 0 :(得分:0)
要使Firebase推送通知在Android上正常工作,以下是您需要在Android应用上执行的操作,
将firebase依赖项添加到gradle文件中,并将从firebase控制台下载的google-services.json文件放入您的项目中。
compile "com.google.firebase:firebase-messaging:17.3.4"
创建一个用于接收推送令牌和通知有效载荷的接收器。
public class FCMNotificationReceiver extends FirebaseMessagingService
{
@Override
public void onNewToken(String s) {
super.onNewToken(s);
// send token to your server.
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// use your own logic here to process notification. For example,
sendNotification(remoteMessage.getNotification().getTitle(),
remoteMessage.getNotification().getBody(), remoteMessage.getData());
}
}
在清单中注册以上服务
<service android:name=".FCMNotificationReceiver">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String mToken = instanceIdResult.getToken();
// send token to your server.
}
});