你好。 我练习了FCM消息,但无法发送消息。 我是基于谷歌代码制作的,但我正在阅读它,但服务器不发送消息,基本控制台只能触发一次。 有人可以帮忙吗?这是代码!
MainActivity:
public class MainActivity extends AppCompatActivity {
static Context context;
static TextView token;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
token = (TextView) findViewById(R.id.token);
}
}
FCMMessagingService
public class FCMMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder);
super.onMessageReceived(remoteMessage);
}
}
FCMInstanceIdService
public class FCMInstanceIdService extends FirebaseInstanceIdService {
public static final String TOKEN = "token";
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
String token = FirebaseInstanceId.getInstance().getToken();
SharedPreferences sp = getApplicationContext().getSharedPreferences(TOKEN, Context.MODE_PRIVATE);
sp.edit().putString(TOKEN, token).commit();
Log.d("TOKEN_NO: ", token);
}
}
清单
<service android:name=".FCMInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name=".FCMMessagingService">
<intent-filter>
<action android:name="com.google.firabase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Project .gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
模块.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "hu.penged.cloudmessage"
minSdkVersion 14
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.google.firebase:firebase-messaging:9.4.0'
}
apply plugin: 'com.google.gms.google-services'
..和server.php ......
<?php
$server_key = "AIzaSyA4ahv6z05t4sbSOZDkw5pX-5EvOHA7zfA";
$adress = "enI7T1-U69o:APA91bHLnlAOJxg8kebr7GcpoVqBf__EHXADofEG-rQiGH60fH3HstCk9BroxoBXjlFEzKJoln-ghATjlcyo4UiCmyDjTeKOKG7NHY1ADiwWxxhg3tvBdWc2J_PbMM0wzKjjpDpgDk6x";
$fcm_server_url = "https://fcm.googleapis.com/fcm/send";
$title = utf8_encode("Notification Test");
$content_text = utf8_encode("Notification message.");
$httpheared = array("Content-Type:application/json", "Authorization:key=".$server_key);
$post_content = array("to" => $adress, "notification" => array("title" => $title, "body" => $content-text));
$curl_connection = curl_init();
curl_setopt($curl_connection, CURLOPT_URL, $fcm_server_url);
curl_setopt($curl_connection, CURLOPT_POST, true);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $httpheared);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, json_encode($post_content));
$valasz = curl_exec($curl_connection);
curl_close($curl_connection);
echo $valasz;
?>
非常感谢你的帮助!!!
答案 0 :(得分:0)
尝试执行以下操作:
在您的server.php脚本而不是notification
param中使用data
。因此它将是:"data" => array("title" => $title, "body" => $content-text)
并且在您的onMessageReceived
方法中,使用remoteMessage.getData().get("title");
获取参数,并使用密钥&#34; body&#34;获取另一个参数的相同代码。
还要检查移动设备收到通知时是否正在调用onMessageReceived方法。
希望这有帮助!