【发布时间】:2017-07-27 12:43:53
【问题描述】:
我是 Android 的新手,我创建了一个程序,该程序将使用 firebase 发送通知,而不使用 firebase 控制台。我使用 php 作为后端,我只有在通过 firebase 控制台发送时才会收到通知,但是当我通过 wamp 服务器发送它我不能。虽然我得到了成功:1.在这里我也分享我的代码和输出。
Index.php
error_reporting(-1);
ini_set('display_errors', 'On');
require_once __DIR__ . '/firebase.php';
require_once __DIR__ . '/push.php';
$firebase = new Firebase();
$push = new Push();
// optional payload
$payload = array();
$payload['title'] = 'Notification!';
$payload['message'] = 'New PickupListed';
// notification title
$title = isset($_GET['title']) ? $_GET['title'] : '';
// notification message
$message = isset($_GET['message']) ? $_GET['message'] : '';
// push type - single user / topic
$push_type = isset($_GET['push_type']) ? $_GET['push_type'] : '';
// whether to include to image or not
$include_image = isset($_GET['include_image']) ? TRUE : FALSE;
$push->setTitle($title);
$push->setMessage($message);
if ($include_image) {
$push->setImage('http://api.androidhive.info/images/minion.jpg');
} else {
$push->setImage('');
}
$push->setIsBackground(FALSE);
$push->setPayload($payload);
$json = '';
$response = '';
if ($push_type == 'topic') {
$json = $push->getPush();
$response = $firebase->sendToTopic('global', $json);
} else if ($push_type == 'individual') {
$json = $push->getPush();
$regId = isset($_GET['regId']) ? $_GET['regId'] : '';
$response = $firebase->send($regId, $json);
}
MessagingService.java
private NotificationUtils notificationUtils;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "===========================messaging 1=======" );
Log.e(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage == null)
return;
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "===========================messaging 2=======" );
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
handleNotification(remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "===========================messaging 3=======" );
Log.e(TAG, "Data Payload: " + remoteMessage.getData());
try {
JSONObject json = new JSONObject(remoteMessage.getData());
handleDataMessage(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
private void handleNotification(String message) {
Log.e(TAG, "===========================messaging 4=======" );
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
Intent intent= new Intent(this,MainActivity.class);
intent.putExtra("message",message);
intent.putExtra("imageUrl",imageUrl);
intent.putExtra("time_stamp",timeStamp);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
final PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("NOTIFICATION !!")
.setContentText(message)
.setAutoCancel(true)
.setSound(notificationSound)
.setContentIntent(pendingIntent) ;
NotificationManager notificationManager=
( NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notifiBuilder.build());
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
}
}
private void handleDataMessage(JSONObject json) {
Log.e(TAG, "===========================messaging 4=======" );
Log.e(TAG, "push json: " + json.toString());
try {
JSONObject data = json.getJSONObject("data");
String title = data.getString("title");
String message = data.getString("message");
boolean isBackground = data.getBoolean("is_background");
String imageUrl = data.getString("image");
String timestamp = data.getString("timestamp");
JSONObject payload = data.getJSONObject("payload");
Log.e(TAG, "title: " + title);
Log.e(TAG, "message: " + message);
Log.e(TAG, "isBackground: " + isBackground);
Log.e(TAG, "payload: " + payload.toString());
Log.e(TAG, "imageUrl: " + imageUrl);
Log.e(TAG, "timestamp: " + timestamp);
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
// app is in foreground, broadcast the push message
Intent intent= new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
final PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("NOTIFICATION data message!!")
.setContentText(message)
.addAction(R.mipmap.ic_accept,"ACCEPT",pendingIntent)
.addAction(R.mipmap.ic_decline,"DECLINE",pendingIntent)
.setAutoCancel(true)
.setSound(notificationSound)
.setContentIntent(pendingIntent) ;
NotificationManager notificationManager=
( NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notifiBuilder.build());
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();
} else {
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
resultIntent.putExtra("message", message);
// check for image attachment
if (TextUtils.isEmpty(imageUrl)) {
showNotificationMessage(getApplicationContext(), title, message, resultIntent);
} else {
showNotificationMessageWithBigImage(getApplicationContext(), title, message, resultIntent, imageUrl);
}
}
} catch (JSONException e) {
Log.e(TAG, "Json Exception: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
private void showNotificationMessage(Context context, String title, String message, Intent intent) {
Log.e(TAG, "===========================messaging 5=======" );
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, intent);
}
但我得到的是输出:仍然没有收到通知
Request:
{"data":{"title":"hey all","is_background":false,"message":"demo","image":"","timestamp":"2017-07-27 12:22:41"}}
Response:
"{\"multicast_id\":8934845196536440484,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1501158164597153%293f606ef9fd7ecd\"}]}"
【问题讨论】:
-
我在您的示例负载中看不到目标。您是将其发送到单个设备还是主题?如果订阅某个主题,您确定设备订阅了该主题吗?
-
@AL。发送到单个设备并且无法创建它应该是目标的位置
标签: php android firebase push-notification firebase-cloud-messaging