Cha*_*san 8 notifications android android-notifications firebase firebase-cloud-messaging
我正在通过我自己的Web服务发送Firebase通知,如下所示.
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = SERVER_KEY ',
'Content-Type: application/json'
);
Run Code Online (Sandbox Code Playgroud)
1.当App在前台和后台没有任何问题时,通知即将到来,但如果我从最近的托盘中删除应用程序然后通知没有到来,我必须处理这个任何解决方案吗?(就像Whatsup通知显示所有场景甚至我强制停止此应用程序的设置)
2.一旦我收到通知,从"onMessageReceived"如何将这些消息,正文内容传递给我的Activity/Fragments?
3.在某些情况下,我想向所有人发送通知,而不是将所有令牌添加到数组中.任何其他方式处理像"发送给所有"这样的东西?
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Data Payload : " + remoteMessage.getData());
sendNotification(remoteMessage.getData().get("message"));
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Firebase Push Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Run Code Online (Sandbox Code Playgroud)
设备失败:( 从Recent Tray中删除后通知不会出现)
成功设备:(从最近的托盘中删除通知后)
1.当App在前台和后台没有任何问题时,通知即将到来,但如果我从最近的托盘中删除应用程序然后通知没有到来,我必须处理这个任何解决方案吗?(就像Whatsup通知显示所有场景甚至我强制停止此应用程序的设置)
根据您的代码,您必须获取空指针异常,remoteMessage.getNotification().getBody()因为您没有发送通知,而是从curl请求发送数据有效负载.根据我的个人经验,您刚才提到的问题尤其在Android Kitkat或以下设备中出现.在棒棒糖或以上Firebase推动似乎工作正常.
2.一旦我收到通知,从"onMessageReceived"如何将这些消息,正文内容传递给我的Activity/Fragments?
您可以onMessageReceived()在活动中发出广播并注册Braodcast接收器,并以广播意图发送数据.示例代码:
private BroadcastReceiver receiver;
@Overrride
public void onCreate(Bundle savedInstanceState){
// your oncreate code
IntentFilter filter = new IntentFilter();
filter.addAction("SOME_ACTION");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
}
};
registerReceiver(receiver, filter);
}
@Override
protected void onDestroy() {
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null
}
super.onDestroy();
}
Run Code Online (Sandbox Code Playgroud)
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getData());
Intent intent=new Intent();
intent.setAction("SOME_ACTION");
intent.putExtra("message", remoteMessage.getNotification().getBody());
sendBroadcast(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
3.在某些情况下,我想向所有人发送通知,而不是将所有令牌添加到数组中.任何其他方式处理像"发送给所有"这样的东西?
Firebase API中没有"发送给所有人",唯一的方法是使用Firebase控制台,它有自己的问题来处理白色图标问题,并翻录通知的自定义参数.
| 归档时间: |
|
| 查看次数: |
5564 次 |
| 最近记录: |