Updating the UI upon receiving an android push notification

℡╲_俬逩灬. 提交于 2019-12-25 01:09:51

问题


I have a query regarding android push notification and i had asked it in another stackoverflow post and i did not get much help out of it [Query regarding Android push notifications. So i am posting it again, and it is as follows:

I have an android app that receives push notifications from Google push notification service. When i tap on the received notification, it opens an UI which displays this message, it is a list view. Now, when the user receives the push notification, and assuming that this screen is open, the UI should be refreshed automatically, such that it displays the latest notification. Could anybody let me know how i can solve this?

Below is my code that i have implemented:

Java code to receive the notification:

import java.util.Timer;
import java.util.TimerTask;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;

import com.example.foodu.R;
import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

    private static final String TAG = "GCM ::Service";

    // Use your PROJECT ID from Google API into SENDER_ID
    public static final String SENDER_ID = "53340195486";

    public GCMIntentService() {
        super(SENDER_ID);
    }

    @Override
    protected void onError(Context arg0, String errorId) {
        Log.e(TAG, "onError: errorId=" + errorId);
    }

    @Override
    protected void onMessage(Context context, Intent data) {
        String message;
        // Message from PHP server
        message = data.getStringExtra("message");
        // Open a new activity called GCMMessageView
        Intent intent = new Intent(this, com.example.foodu.Notification.class);
        // Pass data to the new activity
        intent.putExtra("message", message);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        // Starts the activity on notification click
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        // Create the notification with a notification builder
        Notification notification = new Notification.Builder(this)
                .setSmallIcon(R.drawable.ic_logo)
                .setWhen(System.currentTimeMillis())
                .setContentTitle("Deals")
                .setContentText(message).setContentIntent(pIntent)
                .getNotification();
        // Remove the notification on click
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(R.string.app_name, notification);

        {
            // Wake Android Device when notification received
            PowerManager pm = (PowerManager) context
                    .getSystemService(Context.POWER_SERVICE);
            final PowerManager.WakeLock mWakelock = pm.newWakeLock(
                    PowerManager.FULL_WAKE_LOCK
                            | PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH");
            mWakelock.acquire();

            // Timer before putting Android Device to sleep mode.
            Timer timer = new Timer();
            TimerTask task = new TimerTask() {
                public void run() {
                    mWakelock.release();
                }
            };
            timer.schedule(task, 5000);
        }

    }

    @Override
    protected void onRegistered(Context arg0, String registrationId) {
        Log.i(TAG, "onRegistered: registrationId=" + registrationId);

    }

    @Override
    protected void onUnregistered(Context arg0, String registrationId) {
        Log.i(TAG, "onUnregistered: registrationId=" + registrationId);
    }

}

The code for the corresponding activity that would be launched when the user taps on the notification:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.TimeZone;

import com.example.foodu.R;
import com.example.foodu.R.drawable;
import com.example.foodu.R.id;
import com.example.foodu.R.layout;
import com.example.foodu.R.menu;
import com.google.android.gcm.GCMRegistrar;

import android.support.v7.app.ActionBarActivity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.util.Log;
import android.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Notification extends ActionBarActivity {

    LinkedList<NotificationData> notificationList = new LinkedList<NotificationData>();
    ListView listView = null;
    NotificationListAdapter adaptor;
    ActionMode mActionMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);

        overridePendingTransition(R.anim.trans_left_in, R.anim.trans_left_out);

        listView = (ListView) findViewById(R.id.listView1);

        // Retrive the data from GCMIntentService.java
        Intent i = getIntent();
        String message = i.getStringExtra("message");
        //getDataForDisplay();
        if(message!=null)
        {
            parseData(message);
        }else{
        getDataToDisplay();
        }
        adaptor = new NotificationListAdapter(getApplicationContext(), notificationList);
        listView.setAdapter(adaptor);
        TextView emptyText = (TextView) findViewById(R.id.empty);
        emptyText.setText("No Events Yet!");
        listView.setEmptyView(emptyText);
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                    int position, long id) {
                onListitemSelect(position);
                view.setSelected(true);
                return true;
            }
        });
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();


        adaptor.notifyDataSetChanged();
    }   

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
    }

    void writeToFile(){
        FileOutputStream fos;
        try {
        fos = openFileOutput("varun", Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(notificationList);
        oos.close();
    }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    void readFromFile(){
        try{
        FileInputStream fis = openFileInput("varun");
        ObjectInputStream ois = new ObjectInputStream(fis);
        LinkedList<NotificationData> local = (LinkedList<NotificationData>) ois.readObject();
        ois.close();

        for (int i = 0; i < local.size(); i++) {
            notificationList.add(local.get(i));
        }

        }catch(Exception e){
            e.printStackTrace();
        }
    }


    private void getDataToDisplay() {
        // TODO Auto-generated method stub
        readFromFile();
    }

    private void parseData(String message) {
        try {
            int len = 0;
            String[] stringArr = new String[100];
            StringTokenizer st = new StringTokenizer(message, ".");
            len = st.countTokens();
            for (int i = 0; i < len; i++) {
                if (st.hasMoreTokens()) {
                    stringArr[i] = st.nextToken();
                }
            }

            NotificationData data = new NotificationData();
            data.title = stringArr[0];
            data.venue = stringArr[1];
            data.date = stringArr[2];
            data.time = stringArr[3];
            notificationList.add(data);
            readFromFile();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void getDateToDisplay() {
        // TODO Auto-generated method stub

    }



    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        writeToFile();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.notificationmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        if(id == R.id.action_register){
            registerDevice();
            return  true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void registerDevice() {
        try {
            GCMRegistrar.checkDevice(this);
            GCMRegistrar.checkManifest(this);

            GCMRegistrar
                    .register(Notification.this, GCMIntentService.SENDER_ID);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.notificationcontext, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

            switch (item.getItemId()) {
            case R.id.menu_calender:
                addToCalender();
                mode.finish();
                return true;
            case R.id.menu_delete:
                //deleteData();
                showAlertBox();
                return false;
            case R.id.menu_share:
                shareDate();
                mode.finish();
                return true;
            case R.id.menu_copy:
                copyToClip();
                mode.finish();
                return true;
            default:
                return false;
            }
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mActionMode = null;
            adaptor.removeSelection();
        }
    };

    void onListitemSelect(int position) {
        adaptor.toggleSelection(position);
        boolean hasCheckedItems = adaptor.getSelectedCount() > 0;

        if (hasCheckedItems && mActionMode == null) {
            mActionMode = startActionMode((Callback) mActionModeCallback);
        } else if (!hasCheckedItems && mActionMode != null) {
            mActionMode.finish();
        }

        if (mActionMode != null)
            mActionMode.setTitle(String.valueOf(adaptor.getSelectedCount()));

    }

    protected void showAlertBox() {
        // TODO Auto-generated method stub
        AlertDialog.Builder builder1 = new AlertDialog.Builder(Notification.this);
        builder1.setMessage("Delete " + adaptor.getSelectedIds().size()+ " events?");
        builder1.setCancelable(true);
        builder1.setIcon(R.drawable.alert);
        builder1.setTitle("Caution");
        builder1.setIcon(android.R.drawable.ic_dialog_alert);
        builder1.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                deleteData();
                mActionMode.finish();
            }
        });
        builder1.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });

        AlertDialog alert11 = builder1.create();
        alert11.show();
    }

    protected void copyToClip() {

        StringBuilder shareText = new StringBuilder();
        for (int i = 0; i < adaptor.getSelectedIds().size(); i++) {
            NotificationData data = notificationList
                    .get(adaptor.getSelectedIds().keyAt(i));
            shareText.append(data.title + " " + data.venue + " " + data.date
                    + " " + data.time);
            shareText.append("\n");
        }

        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("Notification App", shareText);
        clipboard.setPrimaryClip(clip);

        Toast.makeText(getApplicationContext(), "Data copied to ClipBoard",
                Toast.LENGTH_LONG).show();
    }

    protected void shareDate() {

        StringBuilder shareText = new StringBuilder();
        for (int i = 0; i < adaptor.getSelectedIds().size(); i++) {
            NotificationData data = notificationList
                    .get(adaptor.getSelectedIds().keyAt(i));
            shareText.append(data.title + " " + data.venue + " " + data.date
                    + " " + data.time);
            shareText.append("\n");
        }
        String share = shareText.toString();
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, share);
        sendIntent.setType("text/plain");
        startActivity(sendIntent);
    }

    protected void deleteData() {
        int count = 0;
        int startPoint = adaptor.getSelectedIds().keyAt(0);
        for (int i = 0; i < adaptor.getSelectedIds().size(); i++) {
            adaptor.remove(notificationList.get(startPoint));
            count++;
        }

        String message = " Event";
        if(count>1)
        {
            message = " Events";
        }
        Toast.makeText(getApplicationContext(),
                count + message+" deleted", Toast.LENGTH_LONG)
                .show();

    }

    private void addToCalender() {

        try {
            int count = 0;
            for (int i = 0; i < adaptor.getSelectedIds().size(); i++) {
                NotificationData data = notificationList
                        .get(adaptor.getSelectedIds().keyAt(i));

                ContentResolver cr = getApplicationContext()
                        .getContentResolver();
                ContentValues values = new ContentValues();

                String myDate = data.date + " " + data.time;

                String timeArr[] = data.time.split("to");

                SimpleDateFormat sfd = new SimpleDateFormat(
                        "' Date: 'MM/dd/yyyy  'Time: 'hh a", Locale.getDefault());

                long time = sfd.parse(myDate).getTime();
                values.put(CalendarContract.Events.DTSTART, time);
                if (timeArr.length > 0) {
                    String endTime = timeArr[1];
                    SimpleDateFormat timeFormat = new SimpleDateFormat(
                            "' Date: 'MM/dd/yyyy hh a", Locale.getDefault());
                    long endtime = timeFormat.parse(data.date + " " + endTime)
                            .getTime();
                    values.put(CalendarContract.Events.DTEND, endtime);
                }

                values.put(CalendarContract.Events.TITLE, data.title);
                values.put(CalendarContract.Events.DESCRIPTION, data.venue);
                TimeZone timeZone = TimeZone.getDefault();
                values.put(CalendarContract.Events.EVENT_TIMEZONE,
                        timeZone.getID());
                values.put(CalendarContract.Events.CALENDAR_ID, 1);
                Uri uri = cr
                        .insert(CalendarContract.Events.CONTENT_URI, values);
                count++;
            }

            String message = " Event";
            if(count>1)
            {
                message = " Events";
            }

            Toast.makeText(getApplicationContext(),
                    count + message + " added to Calender", Toast.LENGTH_LONG)
                    .show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

回答1:


Use LocalBroadcastManager

Check following code / steps

1) Add in your activity (UI refresh Activity)

private BroadcastReceiver mMyBroadcastReceiver;

Then ,

2) In onResume

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    mMyBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) 
    {
                // Here you can refresh your listview or other UI
            Toast.makeText(getApplicationContext(), "Receiver", 2000).show();
        }
    };
try {

LocalBroadcastManager.getInstance(this).registerReceiver(mMyBroadcastReceiver,new IntentFilter("your_action"));

} catch (Exception e) 
{
        // TODO: handle exception
        e.printStackTrace();
}}

// and your other code

3) Then unregister in onPause

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mMyBroadcastReceiver);
}

4) Finally add in your GCM reciver class. first check your activity is Visible or not using static variable

if visible add
       Intent gcm_rec = new Intent("your_action");  LocalBroadcastManager.getInstance(arg0).sendBroadcast(gcm_rec);
else
    use Notification Manager for notification.

I think this is easy and best way to refresh your listview UI / call Fetching method.



来源:https://stackoverflow.com/questions/27240817/updating-the-ui-upon-receiving-an-android-push-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!