收到Android推送通知后更新UI [英] Updating the UI upon receiving an android push notification

查看:91
本文介绍了收到Android推送通知后更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于android推送通知的查询,我在另一个stackoverflow帖子中问过,但没有得到太多帮助[

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:

我有一个Android应用,可从Google推送通知服务接收推送通知.当我点击收到的通知时,它会打开一个显示此消息的UI,它是一个列表视图.现在,当用户接收到推送通知时,并假设此屏幕已打开,则应该自动刷新UI,以使其显示最新的通知.有人可以让我知道如何解决这个问题吗?

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代码:

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)添加您的活动(UI刷新活动)

1) Add in your activity (UI refresh Activity)

private BroadcastReceiver mMyBroadcastReceiver;

然后,

2)在onResume

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

//和您的其他代码

3)然后在onPause中注销

3) Then unregister in onPause

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

4)最后,添加您的GCM接收器类. 首先检查您的活动是否可见或不使用静态变量

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.

这篇关于收到Android推送通知后更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆