简体   繁体   English

如何设置FCM通知图标

[英]How to Set FCM notification Icon

I am using php curl to push notification, Help me adding icon to my notification 我正在使用php curl推送通知,请帮助我在通知中添加图标
i am using below php code for push notification 我正在使用以下php代码进行推送通知

Using cordova firebase plugin in my app 在我的应用程序中使用Cordova Firebase插件
Working on a hybrid app using phonegap,cordova 使用phonegap,cordova来开发混合应用程序
Currently Just doing this for android app 目前只是为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 );
}

Ty this: 请输入:

fcm push notification code using android app 使用Android应用程序的fcm推送通知代码

We have created to classes: 我们创建了以下类:

  • Notification class 通知类别
  • Token service Class 代币服务等级

Fcm Notification Service Class 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());
}
  }

Token service class 代币服务等级

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);

  }
  }

it helps you 它可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM