星期三, 10月 31, 2018

[three.js] 南北極模糊問題

一行指令解決南北極模糊

var maxAnisotropy = this.renderer.getMaxAnisotropy();

texture.anisotropy = maxAnisotropy;


https://www.jianshu.com/p/9b2702987dd2

星期一, 10月 22, 2018

[FCM] firebase admin 初探筆記: 從backend丟訊息

記錄一下nodejs使用FCM Admin的相關筆記,
主要要透過自已的backend與fcm做溝通,
跟web notification無關。

1. 安裝


npm install firebase-admin --save

2. Firebase開專案


3. 設定金鑰

專案設定頁->點擊服務帳戶的Tab->產生新的私密金鑰



4. 初始化SDK


var admin = require('firebase-admin');
var serviceAccount = require('path/to/serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'https://.firebaseio.com'
});


當然如果不要透過下載.json,也是可以用變數的方式做初始化。


5. 發送訊息 send()

接著就可以透過admin.messaging()內的send方法發送訊息

// The topic name can be optionally prefixed with "/topics/".
var topic = 'highScores';
// See documentation on defining a message payload.
var message = {
  data: {
    score: '850',
    time: '2:45'
  },
  topic: topic};
// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

也可以在send方法內使用測試模式

admin.messaging().send(message, dryRun)

指定條件的訊息

在message也可以加condition

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
var condition = "'stock-GOOG' in topics || 'industry-tech' in topics";
// See documentation on defining a message payload.
var message = {
  notification: {
    title: '$GOOG up 1.43% on the day',
    body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.'
  },
  condition: condition};
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
admin.messaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

定義訊息

透過fcm可以推播包含webpush/apns(iOS)/android等消息。

一個封裝訊息的欄位參數

参数说明
data键值对映射,其中所有键和值都是字符串。
notification一个包含 title 和 body 字段的对象。
android一个由 Android 消息专用字段组成的对象。要了解详情,请参阅 Android 专用字段
apns一个由 Apple 推送通知服务 (APNS) 专用字段组成的对象。要了解详情,请参阅 APNS 专用字段
webpush一个由 WebPush 协议专用字段组成的对象。要了解详情,请参阅 WebPush 专用字段
token一个用于标识消息的收件人设备的注册令牌。
topic要将消息发送至的主题名称。该主题名称不能包含 /topics/ 前缀。
condition发送消息时所依据的条件,例如 "foo" in topics && "bar" in topics

綜合的訊息

以下是一個主題的推播,並推發到andriod/apns



var message = {
  notification: {
    title: '$GOOG up 1.43% on the day',
    body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
  },
  android: {
    ttl: 3600 * 1000,
    notification: {
      icon: 'stock_ticker_update',
      color: '#f45342',
    },
  },
  apns: {
    payload: {
      aps: {
        badge: 42,
      },
    },
  },
  topic: 'industry-tech'
};

常見授權錯誤

如果你在send訊息出現以下錯誤,

Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error

通常都是你目前環境的時間跟FCM的不一致,
只要同步時間就好了,

$>sudo service ntp restart

官方文件
https://firebase.google.com/docs/admin/setup?authuser=2
https://firebase.google.com/docs/cloud-messaging/admin/send-messages?authuser=1

星期一, 3月 26, 2018

[react.js] react-redux 使用流程

實作大多採用react-redux來整合原本的redux,
對於第一次入手的人感覺就是要懂一堆第三方套件後才能好好寫。
自已久久寫一下也會忘記大部份的邏輯,所以記錄一下。


目錄結構

actions =>放觸發的action定義
   -index.js =>負責匯出各redux的動作

constants => app state,
  -models 把各別的state 分類
      -index.js 匯出每個個別的app state
  -ActionTypes.js 匯出action要用的變數

reducers =>定義所有reducers
  -ui
    -UIReducer.js => Layout的reducer
  -index.js =>匯出所有reducers並combine所有reducer

store
  - index.js => 匯入所有app reducers與產生一個app store

產生一個新的redux動作


  1. 到constants/models定義新的redux state
  2. 到constants/index.js 匯出剛剛定義的 reduxstate
  3. 到ActionType.js加新的redux事件
  4. 到actions新增一個對應的 <你的新redux命名>Actions.js 檔案
  5. 到reducers新增一個對應的reducer,<你的新redux命名>Reducer.js 檔案
  6. 到reducers/index.js 匯入剛剛新加的reducer
  7. 到containers下找對應的container元件呼叫要使用的actions並匯入

    1. 登入元件範例    







星期二, 2月 20, 2018

[Git] 讓merge不用再輸入訊息


最近在處理shell使用git merge都會跳出上圖所示的的訊息互動視窗

會讓整個想透過sh自動化無法完成,還好可以加上--no-edit參數就可以避過啦。
以下是最完整的語法:

git merge --no-ff develop --no-edit



星期五, 1月 05, 2018

[Git] 從https 的clone變更為ssh

由於先前使用https來clone repo,如果要變動的可以透過以下指令變更回ssh

git remote set-url origin git@github.com:username/your-repository.git

星期六, 11月 25, 2017

[Bootstrap 3] 在行動裝置重新排序Column

剛好做客戶有遇到,要在手機layout的改變column的位置,
簡單來說就左右調換,其實bootstrap裡面就有支援行 re-ordering了。
可以參考以下範例

https://codepen.io/Schmalzy/pen/iJdgD

星期三, 11月 08, 2017

[bootstrap 3] 如何簡單快速覆寫bootstrap的樣式

如果懶的用less來重build bootstrap的樣式的話,
通常你會在link下面多引用一個你要覆寫bootstrap.min.css的客製化樣式。
但你會發現有可能覆寫失敗,
有些人會建議使用大絕招!important 屬性,
找到stackoverflow另一篇建議:
https://stackoverflow.com/questions/8084964/how-to-overwrite-styling-in-twitter-bootstrap/20542297#20542297
因為css選擇器有權重的特性,
可以加上id的名稱去覆寫,這樣就不失彈性啦!

例如自訂一個nav-customer來覆寫nav的相關設定
header #nav-customer{
background-color: rgba(0, 0, 0, 0.6);
}

參考
https://stackoverflow.com/questions/8084964/how-to-overwrite-styling-in-twitter-bootstrap/20542297#20542297

星期日, 11月 05, 2017

[AngularJS] 如何覆寫舊的路由狀態state

由於有客戶客製化的需求,
需要改寫原本路由的介面,
但為了共用核心不變,
找到了可以在run的時候覆寫舊的路由的方法:D

angular.module('module2', ['ui.router'])
.run(['$state', function($state){
  var state = $state.get('先前的路由命名');
  state.templateUrl = '新的樣版';

}]);

星期日, 10月 22, 2017

[react.js] 初學筆記

記錄一下react.js學習上要懂的ecosystem,
做一下自已要速查的note

npm套件管理工具

node.js用來裝需求的套件的工具,安裝完的套件可以給node.js後端用或透過webpack ES6方法來載入套件

webpack build tool (可以用Gulp+browserify)

因為早期都用gulp,但webpack出到v3了,這次就換試webpack這個工具

星期三, 10月 18, 2017

[react.js] 雷雷小伙伴之坑

記錄一下菜B巴react.js之旅的error


Warning: Invalid DOM property `class`. Did you mean `className`?

就是JSX裡面用了html的保留字class啦,要用className

Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object


//死亡的原因是import加了{}
// import { App}  from './components/App';
import  App  from './components/App';

星期三, 10月 11, 2017

[node.js] 如何切換development與production模式

有關node.js處理NODE_ENV來切換developer/production的方法種類

export NODE_ENV=production
Or if you are in windows you could try this:
SET NODE_ENV=production
or you can run your app like this:
NODE_ENV=production node app.js
You can also set it in your js file:
process.env.NODE_ENV = 'production';
https://stackoverflow.com/questions/9198310/how-to-set-node-env-to-production-development-in-os-x

星期一, 8月 28, 2017

[node.js] 如何在node.js做好logger的監控

剛開始寫node.js,還是習慣先找一個logger的套件來trace系統。
這篇文章:node-js-logging-tutorial(也完整說明各種log的使用方法)介紹了winston這個套件,有興趣的新手可以試一下。

https://github.com/winstonjs/winston

[node.js] 快快樂樂學node.js (五) 開始連接mysql

現在要來說明如何連結mysql,
這裡使用node.js的mysql套件(https://github.com/mysqljs/mysql#introduction),可以簡單進行與mysql進行連線。

星期三, 8月 16, 2017

[node.js] 快快樂樂學node.js (四) REST API 模組化-使用控制器


在上一篇
[node.js] 快快樂樂學node.js (三) 實作第一個REST API (沒有採用資料庫)

的範例中,並沒有將程式碼考慮到如果有愈多不到資源的REST要處理的話(例如:用戶/Blog等等),
程式碼就會顯示的不好管理,因此我們要將messages拆分到另一個js檔案裡

[node.js] 快快樂樂學node.js (三) 實作第一個REST API (沒有採用資料庫)

由於大家開始習慣所謂的SPA 單一頁面應用程式架構,
前端可以搭任何喜歡的前端框架,例如vue.js/angular/react.js
因此後端就只要負責與前端透過REST API溝通,
REST API的理論就不多說明了,直接記錄在node.js express如何實作

星期二, 8月 15, 2017

[node.js] 快快樂樂學node.js (二) 如何設定自動重啟

先前有說到如果你沒有用任何套件的話,
基本上修改程式過程是很麻煩的,
因此要裝一些套件來達到這件事,
而且一個完整的範例,要包含前後端
早期有一些grunt套件,目前個人則偏愛用gulp相關套件:D

[node.js] 快快樂樂學node.js (一) 啟動第一個express環境

閱讀jollen大大的node.js express的學習筆記,
方便自已速查,有興趣的朋友可以參閱。
github: https://github.com/jollen/nodejs-fullstack-book
我只從express章節開始記錄!!

其他你感興趣的文章

Related Posts with Thumbnails