国内手机必须要安装以下引用:
image.png1. 第一步 打开FCM官网
image.png2. 选择Android
image.png3. 写入你的APP的包名,应用别名可以不填,还有你app的SHA1值
image.png4. 点击下载JSON文件并且保存在你项目中
image.png这一步下面也有说(配置文件)
image.png点击下一步 选择你创建的应用
image.png下一步下一步略过....
到了发布消息的死磕了!点击审核,点击发布,自动跳转到你发送消息的页面,之后你的app如果在后台就会收到消息
相应配置
在你的app里面集成
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
implementation 'com.google.firebase:firebase-analytics:17.2.0'
implementation 'com.google.firebase:firebase-messaging:20.0.0'
项目根部
classpath 'com.google.gms:google-services:4.3.2'
在你的清单文件里面集成这些
<meta-data //你的推送栏图标,自定义
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/appicon" />
<meta-data //颜色
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/styleTitleOrange" /> <!-- [END fcm_default_icon] -->
<meta-data // 这个可选,
android:name="firebase_messaging_auto_init_enabled"
android:value="false" />
<meta-data //可选
android:name="firebase_analytics_collection_enabled"
android:value="false" />
<service //这是我自定义继承了谷歌的消息类
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
自定义MyFirebaseMessagingService
//继承FirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private String mToken;
@Override
public void onCreate() {
super.onCreate();
L.e("消息服务已开启");
}
//获取到谷歌到token
@Override
public void onNewToken(@NonNull String token) {
super.onNewToken(token);
Log.e("Google token", "Refreshed token: $token");
sendRegistrationToServer(token);
}
// 回传给服务器操作
private void sendRegistrationToServer(String token) {
mToken = token;
//这里我做了本地保存,你可以在你需要到地方获取
ShareUtils.putString(getApplicationContext(), "GoogleToken", mToken);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
L.e("APP处于前台消息标题" + remoteMessage.getNotification().getTitle());
L.e("APP处于前台消息内容" + remoteMessage.getNotification().getBody());
L.e("Data消息(为空)" + remoteMessage.getData());
L.e("服务器" + remoteMessage.getFrom());
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
String token = task.getResult().getToken();
//谷歌返回的token
Log.e("Google_token", token);
}
});
//这个应该可以看懂
if (remoteMessage.getNotification() != null && remoteMessage.getNotification().getBody() != null) {
sendNotification(getApplicationContext(), remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
} else {
sendNotification(getApplicationContext(), remoteMessage.getData().get("title"), remoteMessage.getData().get("body"));
}
}
@Override
public void onDeletedMessages() {
super.onDeletedMessages();
}
@Override
public void onMessageSent(String s) {
super.onMessageSent(s);
}
@Override
public void onSendError(String s, Exception e) {
super.onSendError(s, e);
}
private void sendNotification(Context iContext, String messageTitle, String messageBody) {
//跳转到你想要跳转到页面
Intent intent = new Intent(this, NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setTicker(messageTitle)//标题
.setSmallIcon(R.mipmap.appicon)//你的推送栏图标
.setContentTitle("notification")
.setContentText(messageBody)//内容
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//判断版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"notification",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
//这里如果需要的话填写你自己项目的,可以在控制台找到,强转成int类型
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
最重要的一点!没有这个你app在前台的状态是接收不到的!
在你创建项目的时候,谷歌会给你一个json你需要放在项目中,(上面有图和真相)
打开json 放入进去,位置随便,这下就可以了
"to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
},
因为前台的消息属于数据类型消息,具体可以看下文档。
还有一点,在你的MainActivity 添加,或者需要的地方。!!
if (intent.extras != null) {
for (s in intent.extras!!.keySet()) {
Log.e("MainActivity", s + "--" + intent.extras!!.get(s))
// 在官网的发送notification 使用高级选项可以自定义 键值对,最终会在getIntent().getExtras()中获取到
}
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
网友评论