我正在使用FCM向Android和iOS应用发送通知。
在Android中,我创建了一项服务来接收消息并在应用处于活动状态且在后台时创建自定义通知
public class MyAndroidFirebaseMsgService extends FirebaseMessagingService {
private static final String TAG = "MyAndroidFCMService";
AsyncTask<Void, Void, Void> myTask;
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
text = remoteMessage.getData().get("text");
title = remoteMessage.getData().get("title");
addNotification()
}
public void addNotification(boolean HasImage)
{
intent = new Intent( getApplicationContext(), MainActivity.class);
intent.setAction(Long.toString(System.currentTimeMillis()));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Resources res = getBaseContext().getResources();
NotificationCompat.Builder mNotificationBuilder ;
mNotificationBuilder = new NotificationCompat.Builder( this)
.setLargeIcon(BitmapFactory.decodeResource(res, R.mipmap.ic_ico))
.setSmallIcon(R.drawable.iconbar)
.setContentTitle(title)
.setContentText(text)
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notifyId, mNotificationBuilder.build());
}
在iOS中,我可以接收由Firebase生成的已完成通知,并接收消息。
extension AppDelegate : FIRMessagingDelegate {
func applicationReceivedRemoteMessage(remoteMessage: FIRMessagingRemoteMessage) {
print("%@", remoteMessage.appData)
var title = ""
var text = ""
for value in remoteMessage.appData
{
if (value.0 as! String) == "title"
{
title = value.1 as! String
}
if (value.0 as! String) == "text"
{
text = value.1 as! String
}
}
self.scheduleLocal(title, text: text)
}
func scheduleLocal(title: String, text: String)
{
let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
if settings!.types == .None
{
return
}
let notification = UILocalNotification()
notification.fireDate = NSDate()
notification.alertBody = text
notification.alertAction = title
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
我的问题是,我可以在Xcode中创建Android服务模拟吗?当app在后台时,你能给我一个小例子来创建通知吗?