ローカル通知を埋め込む

アプリケーションのアンインストールに関する最悪のことは何ですか? そうです、聴衆とのコミュニケーションが失われ、アプリケーションが登録アドレスとパッケージ名を変更したことを通知できないことです。 このタスクはプッシュ通知によって解決されますが、かなり面倒であり、必ずしも便利ではありません。 そして、時には高価です。







そのため、ドラムのようにシンプルで、タンクとして信頼できる独自のローカルプッシュ通知を作成します。 コードは普遍的であることが判明し、原則として、たとえば、アプリケーションの新しいバージョンがリリースされたニュースレターや、評価以外の目的で、通常の「プッシュ」の代替として使用できます。







構成から始めましょう。 これはjson形式のプレーンテキストファイルです。



{ "notifications": [ { "id": 1 , "title": "Sorry we are deleted from GPlay" , "text": "Please click for download new app" , "version": 105 , "action": "market://search?q=freemp" , "locale": "ru_Ru" } ] }
      
      







どこで

-id:メッセージの一意の番号

-タイトル:メッセージのタイトル

-テキスト:メッセージのテキスト

-バージョン:すべてのメッセージを設定しない場合(オプション)

-アクション:デフォルトアクション(オプション)

-ロケール:オプション



たとえば、このファイルをgithubに配置して、リンクを追加します。



public static final String MESSAGEURL = " github.com/recoilme/freemp/blob/master/message.json?raw=true ";



次に、メインアクティビティのonCreateエリアのどこかで呼び出します。

新しいUpdateUtils(アクティビティ);



できた 設定は終了しました。 さて、それはどのように機能しますか。



起動時に、アプリケーションは通知の配列をチェックし、以前に表示されなかった通知から次のメッセージを表示します。

次の開始時-通知がなくなるまで以下を表示します。 これにより、メッセージをキューに入れることができます。 まだ正確な理由はわかりませんが、確かに役立ちます。



フィルターには2つのタイプがあります。

バージョン-設定されている場合、たとえば105-アプリケーションのバージョン105でのみ表示されます。 たとえば、バージョン105のユーザー向けに、バージョン106がリリースされたことを書くことができます)

locale-テキストロケール、たとえばロシア語を話すユーザー向けにロシア語でメッセージを作成する必要がある場合



actionパラメーターはオプションです。これは、ユーザーが通知をクリックしたときに呼び出されるUriです。 たとえば、市場や別のアクティビティ(自分のアクティビティを含む)に送信できます。



実際にはそれだけです。 簡単に言えば、同時に、可能な除去による損傷を軽減します。 さて、またはユーザーとの対話を確立します。



動作中のコードは、 https//github.com/recoilme/freempにあります。

しかし実際には、これは1つのファイルです。 ライブラリが開発される可能性があるため、更新プログラムなどをダウンロードする機能を追加します。 それまでの間、コードはここでも快適にフィットします。



 package org.freemp.android; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.AsyncTask; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.text.TextUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Locale; /** * Created by recoil on 06.08.14. * example file: { "notifications": [{ "id":1, "title":"Sorry we are deleted from GPlay", "text":"Please click for download new app", "version":105 , "action":"market://search?q=freemp", "locale":"ru_Ru"}] } - id: unique number of message - title: title of message - text: text of message - version: if not set message for all (may be not set) - action: default action (may be not set) - locale: may be not set */ public class UpdateUtils { public static final String MESSAGEURL = "https://github.com/recoilme/freemp/blob/master/message.json?raw=true"; private Context context; private Activity activity; private int versionCode; private String locale; public UpdateUtils(Activity activity) { this.activity = activity; context = activity.getApplicationContext(); new Update().execute(); } private class Update extends AsyncTask<Void,Void,String> { @Override protected String doInBackground(Void... params) { try { versionCode = context.getPackageManager() .getPackageInfo(context.getPackageName(), 0).versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } locale = Locale.getDefault().toString(); String response = ""; DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(MESSAGEURL); try { HttpResponse httpResponse = client.execute(httpGet); InputStream content = httpResponse.getEntity().getContent(); BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); String s = ""; while ((s = buffer.readLine()) != null) { response += s; } } catch (Exception e) { e.printStackTrace(); } return response; } @Override protected void onPostExecute(String result) { if (!TextUtils.equals("",result)) { JSONObject jsonResult = null; try { jsonResult = new JSONObject(result); } catch (JSONException e) { e.printStackTrace(); return; } //process notifications if exists JSONArray notifications = jsonResult.optJSONArray("notifications"); if (notifications==null) { return; } if (context==null) { return; } //string with showed messages String showedMessages = PreferenceManager.getDefaultSharedPreferences(context).getString(MESSAGEURL,""); for (int i=0;i<notifications.length();i++) { JSONObject jsonNotification = notifications.optJSONObject(i); if (jsonNotification==null) break; final int version = jsonNotification.optInt("version",-1); if (version>0 && version!=versionCode) { continue; } final String localeTarget = jsonNotification.optString("locale","all"); if (!TextUtils.equals("all",localeTarget) && !TextUtils.equals(localeTarget,locale)) { continue; } final int id = jsonNotification.optInt("id"); if (showedMessages.contains(id+";")) { continue; } else { showedMessages+=id+";"; PreferenceManager.getDefaultSharedPreferences(context).edit().putString(MESSAGEURL,showedMessages).commit(); Intent intent = null; if (!TextUtils.equals("",jsonNotification.optString("action",""))) { // if has action add it intent = new Intent(Intent.ACTION_VIEW, Uri.parse( jsonNotification.optString("action",""))); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } else { // if no - just inform intent = new Intent(activity,activity.getClass()); } PendingIntent pIntent = PendingIntent.getActivity(context, id, intent, 0); // if you don't use support library, change NotificationCompat on Notification Notification noti = new NotificationCompat.Builder(context) .setContentTitle(jsonNotification.optString("title","")) .setContentText(jsonNotification.optString("text","")) .setSmallIcon(R.drawable.icon)//change this on your icon .setContentIntent(pIntent).build(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); // hide the notification after its selected noti.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(id, noti); break; } } } } } }
      
      







それが作成された目的のためにそれを使用することは誰にとっても有用ではない場合、私はうれしいです)



All Articles