標籤

2015年11月2日 星期一

Notification通知訊息基本應用-以簡易登入訊息實作

Notification通知訊息

通知訊息幾乎是每個App都有的功能這一次我們就以登入訊息實作的方式來練習,首先登入畫面的部份我們可以使用android studio內建的模板來用


























建立畫面如下





























用這個範例的原因是因為google有許多的模板可以使用可藉由這個練習來閱讀原始碼並學習怎麼串接別人的程式

在主程序中通常都是以oncreate起頭所以從這開始找會比省事
在這之中可以看到這一行
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);mEmailSignInButton.setOnClickListener(new OnClickListener() {
    @Override    public void onClick(View view) {
        attemptLogin();    }
});

根據前一篇Button介紹-以BMI計算實作中我們可以知道這就是button的啟動listener
可以在這直接寫入Notification語法又或者可以繼續追蹤下去

直到追蹤到符合流程的所在地
照理來說應該事先判斷帳密有無錯誤完後才發送訊息
 if (cancel) {

// There was an error; don't attempt login and focus the first

// form field with an error.

focusView.requestFocus();

} else {

// Show a progress spinner, and kick off a background task to

// perform the user login attempt.

showProgress(true);

mAuthTask = new UserLoginTask(email, password);\\驗證帳密

mAuthTask.execute((Void) null);
}



所以寫在else中應該是符合邏輯的
final int notifyID = 1; // 通知的識別號碼

final Notification.InboxStyle inboxStyle = new Notification.InboxStyle(); // 建立InboxStyle

final EditText emmsg=(EditText) findViewById(R.id.email);

final String[] lines = new String[] { "您剛登入了"+emmsg.getText() }; // InboxStyle要顯示的字串內容

inboxStyle.setBigContentTitle("Welcome to login :"); // 當InboxStyle顯示時,用InboxStyle的setBigContentTitle覆蓋setContentTitle的設定

inboxStyle.setSummaryText("更多新訊息"); // InboxStyle的底部訊息

for (int i = 0; i < lines.length; i++) {

inboxStyle.addLine(String.format("%d: %s", i + 1, lines[i])); // 將字串加入InboxStyle

}



final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 取得系統的通知服務

final Notification notification = new Notification.Builder(getApplicationContext()).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("內容標題").setContentText("內容文字").setStyle(inboxStyle).build(); // 建立通知

notificationManager.notify(notifyID, notification); // 發送通知



如果說要照這樣解讀的話應該就能寫的出來

Notification主要要先給予他一組識別編號ID,接著建立inboxstyle物件,這裡emmsg變數是用來接收email欄位的所以登入後會產生登入帳號etBigContentTitle則是可以對內容標題做修改,在用一個for迴圈做字串加入,最後取得服務通知、建立通知並發送及可



最後的效果如下,但是點下通知是沒有效果的,下一回在來寫如何點下通知會產生效過







沒有留言:

張貼留言