Java FCM发送通知Firebase不通过PHP显示前台
manifest.xmlJava FCM发送通知Firebase不通过PHP显示前台,java,php,android,firebase,firebase-cloud-messaging,Java,Php,Android,Firebase,Firebase Cloud Messaging,manifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="im.delight.android.examples.webview" android:versionCode="1" android:versionName="1.0"> <
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="im.delight.android.examples.webview"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="Label"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:label="Label">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name=".MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
part of mainactivity.java
package im.delight.android.examples.webview;
import im.delight.android.webview.AdvancedWebView;
import android.webkit.WebChromeClient;
import android.widget.Toast;
import android.webkit.WebView;
import android.view.View;
import android.webkit.WebViewClient;
import android.graphics.Bitmap;
import android.content.Intent;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.app.Activity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import androidx.annotation.NonNull;
import android.util.Log;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
public class MainActivity extends Activity implements AdvancedWebView.Listener {
private static final String TAG = "MainActivity"; String TEST_PAGE_URL = "https://example.com";
private AdvancedWebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
//bundle must contain all info sent in "data" field of the notification
}
mWebView = (AdvancedWebView) findViewById(R.id.webview);
mWebView.setListener(this, this);
mWebView.setGeolocationEnabled(false);
mWebView.setMixedContentAllowed(false);
mWebView.setCookiesEnabled(true);
mWebView.setThirdPartyCookiesEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
Toast.makeText(MainActivity.this, "Se încarcă...", Toast.LENGTH_SHORT).show();
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
Toast.makeText(MainActivity.this, title, Toast.LENGTH_SHORT).show();
}
});
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
//To do//
return;
}
String token = task.getResult().getToken();
String msg = getString(R.string.fcm_token, token);
String str= msg;
final String yeh = (str.substring(11));
Log.d(TAG, msg);
PHP
当应用程序在后台/未运行时工作正常,但当应用程序在前台运行时,它不会显示通知
我做错了什么?我尝试使用消息,但我需要刷新应用程序(先放在后台,然后放在前台),以便消息出现
请帮忙
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "com.domain.com"
minSdkVersion 16
targetSdkVersion 21
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(':library')
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.google.firebase:firebase-analytics:17.5.0'
implementation 'com.google.firebase:firebase-storage-ktx:19.2.0'
implementation 'com.google.firebase:firebase-messaging-ktx:20.3.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
androidTestImplementation "com.android.support:support-annotations:28.0.0"
implementation 'com.google.firebase:firebase-inappmessaging-display-ktx:19.1.1'
}
define( 'API_ACCESS_KEY', 'x');
// target device 'fcm' id
$device[0]='x';
$url = 'https://fcm.googleapis.com/fcm/send';
$data = array("registration_ids" => $device,
"notification" => array(
"title" => "text",
"body" => "text",
'icon'=>'url',
"sound" => "default"
),
"data"=>array(
"name"=>"xyz",
'image'=>'uurl'
)
);
$data_string = json_encode($data);
$headers = array ( 'Authorization: key=' . API_ACCESS_KEY, 'Content-Type: application/json' );
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL,$url );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($ch);