简体   繁体   English

收到Android推送通知后更新用户界面

[英]Updating the UI upon receiving an android push notification

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 . 我有一个关于android推送通知的查询,我曾在另一个stackoverflow帖子中问过它,但没有得到很多帮助[ 关于Android推送通知的查询 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. 我有一个Android应用,可从Google推送通知服务接收推送通知。 When i tap on the received notification, it opens an UI which displays this message, it is a list view. 当我点击收到的通知时,它会打开一个显示此消息的UI,它是一个列表视图。 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. 现在,当用户接收到推送通知时,并假设此屏幕已打开,则应该自动刷新UI,以使其显示最新的通知。 Could anybody let me know how i can solve this? 有人能让我知道如何解决这个问题吗?

Below is my code that i have implemented: 以下是我已实现的代码:

Java code to receive the notification: 接收通知的Java代码:

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

}

Use LocalBroadcastManager 使用LocalBroadcastManager

Check following code / steps 检查以下代码/步骤

1) Add in your activity (UI refresh Activity) 1)添加您的活动(UI刷新活动)

private BroadcastReceiver mMyBroadcastReceiver;

Then , 然后 ,

2) In onResume 2)在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 3)然后在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. 4)最后,添加您的GCM接收器类别。 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. 我认为这是刷新listview UI /调用Fetching方法的简便且最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM