如何设置 FCM 通知图标



我正在使用php curl来推送通知,帮助我在通知中添加图标,我正在使用下面的php代码进行推送通知


在我的应用程序中
使用cordova firebase插件 使用Phonegap开发混合应用程序,科尔多瓦
目前只是为安卓应用程序执行此操作

function sendFCM($title,$message, $id,$additional_data="") {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => $id, //Device Ids
'data' => array (
"additional_data"  =>$additional_data
),
'notification' => array(
'title' => $title,
'body' => $message,
'sound'=> 'default'
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "Server 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, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
}

Ty 这个:

使用安卓应用程序的 FCM 推送通知代码

我们已经创建了类:

  • 通知类
  • 令牌服务类

FCM 通知服务类

public class NotificationService extends FirebaseMessagingService {
private static final String TAG = "FCM";
boolean notification, sound, vibration;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, remoteMessage.getNotification().getBody()+"");
Log.d(TAG, remoteMessage.getNotification().getTitle()+"");
Log.d(TAG, remoteMessage.getFrom()+"");
Log.d(TAG, remoteMessage.getData()+"");
addNotification(remoteMessage.getNotification());
}
private void addNotification(RemoteMessage.Notification data) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)  // add icon 
.setContentTitle(data.getTitle()+"")
.setAutoCancel(true)
.setContentText(data.getBody()+"");
Notification notification = new Notification();
if (sound)
notification.defaults |= Notification.DEFAULT_SOUND;
if (vibration)
notification.defaults |= Notification.DEFAULT_VIBRATE;
builder.setDefaults(notification.defaults);
Intent notificationIntent = new Intent(this, Service_confirmedActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}

令牌服务类

public class TokenService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";
public static String DeviceToc = "";
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e(TAG, "Refreshed token: " + refreshedToken);
DeviceToc = refreshedToken;
Log.e("DeviceToc",""+refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("deviceToc",token); // Storing string
editor.commit();
Log.i("token",""+token);
}
}

它可以帮助你

最新更新