我可以成功地将Firebase控制台的通知发送到我的设备,但是当我尝试从Java代码执行此操作时,我收到了一个非常无用的错误:
04-12 20:18:09.284 17549-17549/com.package.example W/System.err: at com.package.example.FCMNotification.pushFCMNotification(FCMNotification.java:102)
这是我的FCMNotification类
public class FCMNotification {
public final static String AUTH_KEY_FCM = "FCM_SERVERKEY";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public static void pushFCMNotification(String deviceToken, String title, String body)
throws IOException, JSONException {
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
JSONObject json = new JSONObject();
json.put("to",deviceToken.trim());
JSONObject info = new JSONObject();
info.put("title", title); // Notification title
info.put("body", body); // Notification body
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}
}
我在我的活动中称这个课程为
FCMNotification obj = new FCMNotification();
obj.pushFCMNotification("DEVICE_TOKEN", "TITLE", "BODY");
修改
我的应用可能无法连接到firebase服务器本身,所以我已经包含了我的清单和Gradle:
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.package.example">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/globalicon" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/cardview_shadow_start_color" />
<service android:name="com.package.example.PushNotification"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name="com.package.example.FCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
摇篮:
apply plugin: 'com.android.application'
android {
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/io.netty.versions.properties'
exclude 'META-INF/INDEX.LIST'
}
signingConfigs {
config {
}
}
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
multiDexEnabled true
applicationId "com.gespanet.global"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 29
versionName "1.2"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
disable 'MissingTranslation'
disable 'ExtraTranslation'
}
dexOptions {
javaMaxHeapSize "4g"
}
repositories {
mavenCentral()
}
dependencies {
compile ('com.google.apis:google-api-services-translate:v2-rev49-1.22.0') {
exclude group: 'com.google.guava'
}
compile ('com.google.cloud:google-cloud-translate:0.11.0-alpha') {
exclude group: 'io.grpc', module: 'grpc-all'
exclude group: 'com.google.protobuf', module: 'protobuf-java'
exclude group: 'com.google.api-client', module: 'google-api-client-appengine'
}
compile 'com.google.android.gms:play-services:10.2.1'
compile 'com.google.firebase:firebase-core:10.2.0'
compile 'com.google.firebase:firebase-messaging:10.2.0'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.parse.bolts:bolts-tasks:1.3.0'
compile 'com.parse:parse-android:1.13.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.facebook.android:facebook-android-sdk:4.+'
}
configurations.all {
exclude group: 'org.json', module: 'json'
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
}
apply plugin: 'com.google.gms.google-services'
来自TRY / CATCHEXCEPTION的完整错误记录CAT:
04-12 23:31:56.027 12815-12815/com.gespanet.global W/System.err: android.os.NetworkOnMainThreadException
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:500)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:483)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:465)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:52)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6397)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6389)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:971)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:482)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:418)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:540)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:64)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6397)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6389)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.037 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: Hongbao
04-12 23:31:56.037 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
04-12 23:31:56.037 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: Hongbao
04-12 23:31:56.037 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
04-12 23:31:56.047 12815-12815/com.gespanet.global I/push: Error is here
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: android.os.NetworkOnMainThreadException
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:500)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:483)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:465)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:52)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6397)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6389)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:971)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:482)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:418)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:540)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:64)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6397)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6389)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.067 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: Hongbao
04-12 23:31:56.067 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
04-12 23:31:56.067 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: Hongbao
04-12 23:31:56.067 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
04-12 23:31:56.067 12815-12815/com.gespanet.global I/push: Error is here
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: android.os.NetworkOnMainThreadException
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:500)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:483)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:465)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:52)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$1$1.done(IndividualFeed.java:6366)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$1$1.done(IndividualFeed.java:6358)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:971)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:482)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:418)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:540)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:64)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$1$1.done(IndividualFeed.java:6366)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$1$1.done(IndividualFeed.java:6358)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
使用Java发送通知。实现这个类:
public class FCMNotification {
// Method to send Notifications from server to client end.
public final static String AUTH_KEY_FCM = "YOUR_SERVERKEY";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public static void pushFCMNotification(final String DeviceIdKey, final String title, final String body) throws Exception {
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
BufferedReader in = null;
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = null;
try {
url = new URL(FMCurl);
} catch (MalformedURLException e) {
Log.i("yoyoyo", "error: 1");
e.printStackTrace();
}
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
Log.i("yoyoyo", "error: 2");
e.printStackTrace();
}
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
try {
conn.setRequestMethod("POST");
} catch (ProtocolException e) {
Log.i("yoyoyo", "error: 3");
e.printStackTrace();
}
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
JSONObject json = new JSONObject();
try {
json.put("to",DeviceIdKey.trim());
} catch (JSONException e) {
Log.i("yoyoyo", "error: 4");
e.printStackTrace();
}
JSONObject info = new JSONObject();
try {
info.put("title", title); // Notification title
} catch (JSONException e) {
Log.i("yoyoyo", "error: 5");
e.printStackTrace();
}
try {
info.put("body", body); // Notification body
} catch (JSONException e) {
Log.i("yoyoyo", "error: 6");
e.printStackTrace();
}
try {
json.put("notification", info);
} catch (JSONException e) {
Log.i("yoyoyo", "error: 7");
e.printStackTrace();
}
OutputStreamWriter wr = null;
try {
wr = new OutputStreamWriter(conn.getOutputStream());
} catch (IOException e) {
Log.i("yoyoyo", "error: 8");
e.printStackTrace();
}
try {
wr.write(json.toString());
} catch (IOException e) {
Log.i("yoyoyo", "error: 9");
e.printStackTrace();
}
try {
wr.flush();
} catch (IOException e) {
Log.i("yoyoyo", "error: 10");
e.printStackTrace();
}
try {
conn.getInputStream();
} catch (IOException e) {
Log.i("yoyoyo", "error: 11");
e.printStackTrace();
}
return null;
}
};
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}