通知已成功发送但未在Firebase中通过Android获取

时间:2017-07-11 12:08:53

标签: php android firebase cakephp notifications

我正在我的数据库中上传视频并发送通知,但通知未在移动设备中接收。

代码:

$header = array(
  'header' => array(
    'Authorization'=>'key='.'AAAABxjSQes:APA91bHy_qL4s0BGkiGyCq57fXYtZxzWpfTzRsHZ9AnULieeP1nScb_vuAIYtXob7LEGvvrwBdue5g24iSGDTJGWUG6YcPnAqVx76EKdb5C2P3kl2mF5SrRDAvPWFv0Uqu-HmmVc4wsH',
    'Content-Type'=>'application/json'
  )
);
$http = new HttpSocket();
$results = $http->post('https://fcm.googleapis.com/fcm/send', '{
  "to" : "cvhXVxTGLl8:APA91bG_iaWHfcka48YwkQPN1kDSIDTk1UAzc-QpHPpnhOPvKBruxnTC_Mxo49EJ3ih2pd9_kGIygs6J_cqMP2ZvoK2I9QVILZH97H8ovsCIuKtDFIr-4jKmBxZJ6E6SeuI_5T3XbfHz",
  "priority" : "high",
  "data": {
    "message": "This is a Firebase Cloud Messaging Topic Message!",
  }

}', $header);

输出:

{"multicast_id":5769969831923920749,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1499773517218412%4e07cf6cf9fd7ecd"}]}
[context] => Array
    (
    )

)

1 个答案:

答案 0 :(得分:1)

结帐Firebase接收代码 检查服务类以获取接收通知,并在清单文件中定义

<强> Mainfest

<service android:name="CustomFirebaseMessagingService">
                <intent-filter>
                    <action android:name="com.google.firebase.MESSAGING_EVENT" />
                    <action android:name=".SplashActivity" />
                </intent-filter>
            </service>

检查服务类CustomFirebaseMessagingService.class

public class CustomFirebaseMessagingService extends FirebaseMessagingService {

        private static final String TAG = CustomFirebaseMessagingService.class.getSimpleName();
        private NotificationManager mNotificationManager;
        private int notificationID = 100;
        private int numMessages = 0;
        String title, message;
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            Log.d(TAG, "From: " + remoteMessage.getFrom());

            if (remoteMessage.getData().size() > 0) {
                Log.d(TAG, "Message data payload: " + remoteMessage.getData());
                title = remoteMessage.getData().get("title");
                message = remoteMessage.getData().get("body");
            }

            // Check if message contains a notification payload.
            if (remoteMessage.getNotification() != null) {
                Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
                title = remoteMessage.getNotification().getTitle();
                message = remoteMessage.getNotification().getBody();
            }
            
			//create custom notification code
            displayNotification(getApplicationContext(),title,message);

        }

        protected void displayNotification(Context context, String PUSH_TITLE, String PUSH_MSG) {
            Log.i("Start", "notification");

            Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);

    	      /* Invoking the default notification service */
            NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);
            mBuilder.setDefaults(Notification.DEFAULT_ALL);
            mBuilder.setContentTitle(PUSH_TITLE);
            mBuilder.setContentText(PUSH_MSG);
            mBuilder.setTicker("Message from Promo Caribbean");
            mBuilder.setSmallIcon(R.drawable.ic_top_notification);
            mBuilder.setLargeIcon(icon);

    	      	/* Increase notification number every time a new notification arrives */
            mBuilder.setNumber(++numMessages);
			NotificationCompat.BigTextStyle notiStyle = new
                        NotificationCompat.BigTextStyle();
                //notiStyle.setBigContentTitle(PUSH_TITLE);
                notiStyle.bigText(PUSH_MSG);
                mBuilder.setStyle(notiStyle);            
    	      	/* Creates an explicit intent for an Activity in your app */
            Intent resultIntent = new Intent(context, MainActivity.class);

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(SplashActivity.class);

    	      	/* Adds the Intent that starts the Activity to the top of the stack */
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(
                            0,
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );

            mBuilder.setContentIntent(resultPendingIntent);
            mBuilder.setAutoCancel(true);
            mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    	      /* notificationID allows you to update the notification later on. */
            mNotificationManager.notify(notificationID, mBuilder.build());
        }
    }