📜 ⬆️ ⬇️

We build in local notifications

What is the worst thing about uninstalling the app? That's right, the loss of communication with the audience and the inability to inform that the application has changed the address of registration and the name of the package. Push notifications solve the task, but it is rather troublesome and not always convenient. And sometimes expensive.



Therefore, we will write our own, simple as a drum and reliable as a tank — local push notifications. The code has turned out to be universal, and in principle, it can be used as an alternative to the usual "push", for example, for the newsletter that a new version of the application has been released or for rating, and for anything.
')


Let's start with the config. This is a plain text file in json-format.

{ "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" } ] } 


Where
- id: unique number of message
- title: title of message
- text: text of message
- version: if not set message for all (optional)
- action: default action (optional)
- locale: optional

We place this file, for example, on a githaba and add a link to it:

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

Then, somewhere in the onCreate area, the main activity is called:
new UpdateUtils (activity);

Is done. With the setting over. Now how it works.

When you start the application checks the array of notifications and shows the following message from those that were not shown earlier.
Upon subsequent start-up, it shows the following and so on until notifications are over. This way you can organize a message queue. I do not know yet exactly why, but it will certainly come in handy.

There are two types of filters:
version - if you specify, for example, 105 - it will be shown only in version 105 of the application. You can, for example, write for version 105 users that version 106 is released)
locale - text locale, for example, if you need to make a message in Russian, for Russian-speaking users

The action parameter is optional, this is the Uri that will be called when you click on the notification. You can, for example, send it to the market or to another activity (including your own).

Actually that's all. Quite simply, and at the same time will allow to level the damage from the possible removal. Well, or to establish a dialogue with users.

The code in action can be found here: https://github.com/recoilme/freemp
But actually this is one file. Perhaps the library will develop, add the ability to download updates or something like that. In the meantime, the code comfortably fits here:

 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; } } } } } } 


I would be glad if it is not useful for anyone to use it for the purposes for which it was created)

Source: https://habr.com/ru/post/232453/


All Articles