【问题标题】:How to Set FCM notification Icon如何设置 FCM 通知图标
【发布时间】:2019-01-26 01:27:42
【问题描述】:

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

在我的应用程序中使用 cordova firebase 插件
使用 phonegap、cordova 开发混合应用程序
目前只是为安卓应用做这个

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

【问题讨论】:

    标签: php android cordova firebase cordova-plugin-fcm


    【解决方案1】:

    写这个:

    使用安卓应用的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);
    
      }
      }
    

    对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多