[英]How to Set FCM notification Icon
我正在使用php curl推送通知,請幫助我在通知中添加圖標
我正在使用以下php代碼進行推送通知
在我的應用程序中使用Cordova Firebase插件
使用phonegap,cordova來開發混合應用程序
目前只是為Android應用程序做
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 );
}
請輸入:
使用Android應用程序的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);
}
}
它可以幫助你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.