2024年11月11日星期一

Clone Github with personal access token

Github doesn't support cloning with username password.

With personal access token (PAT):

git clone https://<PAT>@github.com/path/repo.git

Implementing Push Notification in iOS apps using Firebase Cloud Messaging

This article shows example codes in both iOS side and server side to let you know how to make use of Firebase Cloud Messaging (FCM) to implement push notification for your iOS app.

You need to use the Firebase for your iOS app.

Not in this article

I will not go into detail on how to setup in Xcode side nor the certification issues. There are tons of articles on the web for those. The steps for this part are same for any platforms.

Basic concept about iOS push notification

Almost every iOS app needs push notification. And you don’t want to deal with complicated details.

Here is the basic components you need to know.

  1. Your iOS app. This faces to human users.
  2. The Apple push notification service. This service, or server, belongs to Apple. It sends the notification to every iOS device upon it receives requests from your application server.
  3. A server application. This server, or a server application, will send requests to the Apple push notification service when it wants to notify users.

If you want to implement the server side logic, it is possible. However, for most of us, we want to get rid of the trouble and here the FCM comes.

Setup in Firebase

We need to do these steps.

  1. Firebase project setup. This is to put the key file of your project to the Firebase project setup for cloud messaging. So the Apple server will recognize the FCM server and allow it talks to the Apple server on behalf of your app.
  2. Firebase functions. To send notifications you need backend logic. Firebase functions is the place you write your backend logic for FCM. To write this code you need to setup the Google Cloud command line tool. Then you write at your local computer and use the tool to deploy it to the Firebase server. Note that you cannot view the code on Firebase console. All you can review is to your source code. So please do some backend or version control for your source.

iOS App setup

On the iOS app, we have several ways to receive messages from the FCM. In this case, for the simplicity, we choose “subscription by topic” approach. The Topic is actually a string, and our app subscribe it via a call:

Messaging.messaging().subscribe(toTopic: topicString)

Please note that the topic string can only accept certain characters as shown in this regular expression:

[a-zA-Z0-9-_.~%]+

Backend logic example

Let me use an example to show how we trigger the push notification.

In this example project, it uses the Realtime database of Firebase. This is a JSON-like structure database and we refer data using path. In this use case, we want to send out a push notification whenever there is a new node (data object) is inserted to the node “/messages”. This is actually a new message is sent by some user to another.

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.sendPush = functions.database.ref('/messages/{pushId}')
.onCreate((snapshot, context) => {
const msgid = context.params.pushId;
const msg = snapshot.val();
const peer = msg.peer ;
const room = msg.chatroomid;
const text = msg.text ;
const sender = msg.userid;
const sendername = msg.username ;
// just in case. old version of messages may not have this field.
if (!peer) {
  return 0;
}
// room id has colon, but it is not valid for firebase topic name.
// this matches what the app subscribes for each room.
const topic = peer + "-" + room.replace(":","-");
const message = {
  notification: {
    title: sendername,
    body: text
  },
  data: {
    to: peer ,
    sender: sender ,
    room: room,
    sendername: sendername,
    text: text
  },
  topic: topic
};
admin.messaging().send(message)
.then((response) => {
  // Response is a message ID string.
  functions.logger.log('Successfully sent message:', response);
})
.catch((error) => {
  functions.logger.log('Error sending message:', error);
});
});

As you see, the key point here is to construct a JSON structure to represent your message to send to your iOS app. Please look at the message variable. It consists of 3 parts.

The notification: This is what displays in the push notification banner on the iOS device.

The data: This is the data you want to send to the iOS app to process with. The name of the fields seem could be any string but please try to avoid using strange ones and be simple, and avoid symbols.

The topic: This is the FCM topic string that your iOS app subscribes to.

Conclusion

We hope this is helpful. Please let me know in comments that you have any doubts. We try to make this writing short. So you would still need to check Firebase documentations to know how it really works.

How to sync swiftdata with iCloud

How to sync swiftdata with iCloud

If you want to sync your app's data to iCloud, go to the Signing & Capabilities settings for your app's target, then:

  • Add the iCloud capability.
  • Select CloudKit from its options.
  • Press + to add a new CloudKit container, or select one of your existing ones.
  • Add the Background Modes capability.
  • Check the box "Remote Notifications" checkbox from its options.

…and that's it: your app is now configured to sync all its data with iCloud.

Tip: Although you can attempt to test iCloud support in the simulator, I've found it rarely works well – it's much better to test on a real device.

Although the configuration is complete, you might need to make some changes to your SwiftData models because CloudKit has a few very specific requirements. Annoyingly, if you don't follow these requirements your iCloud sync will silently fail, so here they are:

  • You cannot use @Attribute(.unique) on any property you want to sync to CloudKit.
  • All properties must either have default values or be marked as optional, alongside their initializer.
  • All relationships must be marked optional. This is Particularly Annoying™, but it's a requirement so there's not much we can do.

additional

  • if you see error saying "cannot modify" something, go iCloud dashboard, create a custom zone in Private database.

source

https://www.hackingwithswift.com/quick-start/swiftdata/how-to-sync-swiftdata-with-icloud

几十亿年前,第一个纳米机器是谁制造的?

2024年11月6日星期三

Apple CEO Tim Cook on How Steve Jobs Recruited Him and More | The Job In...


一件有趣事實是,Steve樂於被說服,只要你真的有好主意。
而且,他喜歡產品,產品,產品。

聽到 Tim 說犧牲了睡眠,我不爭氣地笑了。我去玩耍了,人家在工作。

2024年11月5日星期二

結婚的意義



今天看到一段話,
說婚姻是你這輩子,
唯一一次親自選擇家人。

你想和什麼樣的人,牽手走完這一生,
就找什麼樣的人。

終有一天你會明白,經濟基礎固然重要,
但更重要的是,一個人的教養,擔當,責任心,控制情緒的能力,
以及說話方式和處事態度。

結婚的意義就是,
萬家燈火,有一盞燈為你而留,
而不是雞飛狗跳有你一個。

伴侶真的很重要,
晚一點沒關係,
但一定得是對的人。

因為他是你後半輩子裡的光。

(摘自網路)

餐肉和垃圾郵件


餐肉和垃圾郵件都叫 spam 。

原來根據 Wikipedia, 垃圾郵件的英語和午餐肉同為「Spam」。語源來自英國搞笑節目《蒙提·派森》中以午餐肉為題的搞笑短劇,說一間餐廳供應的餐點全是午餐肉,而且還有一群維京人打扮的食客唱著不斷重覆「Spam」這歌詞的讚歌,令人煩厭。

有趣 。

精選

Clone Github with personal access token

Github doesn't support cloning with username password. With personal access token (PAT): git clone https:// <PAT> @ github.com/pa...