gpt4 book ai didi

android - 我在FirebaseInstanceIdServices中收到错误

转载 作者:行者123 更新时间:2023-11-30 04:53:46 25 4
gpt4 key购买 nike

FirebasePluginInstanceIDService

package org.apache.cordova.firebase;

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class FirebasePluginInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG = "FirebasePlugin";

FirebasePlugin.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String newToken = instanceIdResult.getToken();
Log.e("newToken", newToken);

}
});
}


FirebasePluginMessagingService

package org.apache.cordova.firebase;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.app.Notification;
import android.text.TextUtils;
import android.content.ContentResolver;
import android.graphics.Color;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;
import java.util.Random;

public class FirebasePluginMessagingService extends FirebaseMessagingService {
private static final String TAG = "FirebasePlugin";

private String getStringResource(String name) {
return this.getString(
this.getResources().getIdentifier( name, "string", this.getPackageName()));
}

@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.e("NEW_TOKEN", refreshedToken);

}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

boolean wasHandled =
FirebasePluginMessageReceiverManager.onMessageReceived(remoteMessage);
if (wasHandled) {
Log.d(TAG, "Message was handled by a registered receiver");

return;
}

String title = "";
String text = "";
String id = "";
String sound = "";
String lights = "";
Map<String, String> data = remoteMessage.getData();

if (remoteMessage.getNotification() != null) {
title = remoteMessage.getNotification().getTitle();
text = remoteMessage.getNotification().getBody();
id = remoteMessage.getMessageId();

} else if (data != null) {
title = data.get("title");
text = data.get("text");
id = data.get("id");
sound = data.get("sound");
lights = data.get("lights"); //String containing hex ARGB color, miliseconds on,
miliseconds off, example: '#FFFF00FF,1000,3000'

if (TextUtils.isEmpty(text)) {
text = data.get("body");
}
}

if (TextUtils.isEmpty(id)) {
Random rand = new Random();
int n = rand.nextInt(50) + 1;
id = Integer.toString(n);
}

Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message id: " + id);
Log.d(TAG, "Notification Message Title: " + title);
Log.d(TAG, "Notification Message Body/Text: " + text);
Log.d(TAG, "Notification Message Sound: " + sound);
Log.d(TAG, "Notification Message Lights: " + lights);

// TODO: Add option to developer to configure if show notification when app on foreground
if (!TextUtils.isEmpty(text) || !TextUtils.isEmpty(title) || (data != null && !data.isEmpty())) {
boolean showNotification = (FirebasePlugin.inBackground() ||
!FirebasePlugin.hasNotificationsCallback()) &&
(!TextUtils.isEmpty(text) ||
!TextUtils.isEmpty(title));
sendNotification(id, title, text, data, showNotification, sound, lights);
}
}

private void sendNotification(String id, String title, String messageBody, Map<String, String>
data, boolean showNotification, String sound, String lights) {
Bundle bundle = new Bundle();
for (String key : data.keySet()) {
bundle.putString(key, data.get(key));
}

if (showNotification) {
Intent intent = new Intent(this, OnNotificationOpenReceiver.class);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id.hashCode(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);

String channelId = this.getStringResource("default_notification_channel_id");
String channelName = this.getStringResource("default_notification_channel_name");
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
notificationBuilder
.setContentTitle(title)
.setContentText(messageBody)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX);

int resID = getResources().getIdentifier("notification_icon", "drawable",
getPackageName());
if (resID != 0) {
notificationBuilder.setSmallIcon(resID);
} else {
notificationBuilder.setSmallIcon(getApplicationInfo().icon);
}

if (sound != null) {
Log.d(TAG, "sound before path is: " + sound);
Uri soundPath = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
getPackageName() + "/raw/" + sound);
Log.d(TAG, "Parsed sound is: " + soundPath.toString());
notificationBuilder.setSound(soundPath);
} else {
Log.d(TAG, "Sound was null ");
}

if (lights != null) {
try {
String[] lightsComponents = lights.replaceAll("\\s", "").split(",");
if (lightsComponents.length == 3) {
int lightArgb = Color.parseColor(lightsComponents[0]);
int lightOnMs = Integer.parseInt(lightsComponents[1]);
int lightOffMs = Integer.parseInt(lightsComponents[2]);

notificationBuilder.setLights(lightArgb, lightOnMs, lightOffMs);
}
} catch (Exception e) {
}
}

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
int accentID = getResources().getIdentifier("accent", "color", getPackageName());
notificationBuilder.setColor(getResources().getColor(accentID, null));

}

Notification notification = notificationBuilder.build();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
int iconID = android.R.id.icon;
int notiID = getResources().getIdentifier("notification_big", "drawable",
getPackageName());
if (notification.contentView != null) {
notification.contentView.setImageViewResource(iconID, notiID);
}
}

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Since android Oreo notification channel is needed.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, channelName,
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}

notificationManager.notify(id.hashCode(), notification);

} else {
bundle.putBoolean("tap", false);
bundle.putString("title", title);
bundle.putString("body", messageBody);
FirebasePlugin.sendNotification(bundle, this.getApplicationContext());
}
}
}


使用命令Ionic Cordova构建android时出现错误。它显示了Firebase插件的错误。我不希望这些插件在没有这些plugins.thanx的情况下有任何方法可以构建apk。这确实是压力很大的代码。尽快给我您的宝贵答案。

enter image description here

最佳答案

因为它已贬值。修复非常简单,只需移动代码即可。

您可以在FirebaseMessagingService子类中将onTokenRefresh()的代码移动到onNewToken()

您可能需要通过cordova-plugin-firebasex使用cordova-plugin-firebase。
 https://github.com/dpa99c/cordova-plugin-firebase#migrating-from-cordova-plugin-firebase

另外寻找这个:

https://firebase.google.com/support/release-notes/android#update_-_april_02_2019 96处,我找到了上一个工作版本,并相应地调整了project.properties,如下所示:

关于android - 我在FirebaseInstanceIdServices中收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59573928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com