使用GCM通知

时间:2017-01-10 12:58:05

标签: android push-notification

我正在尝试通过点击Android应用中的通知在我的应用中打开详细信息页面,但在每次通知点击时,它会显示相同的数据,即上次通知的数据。我从服务器请求获得了一些值&这些是正确收到的。唯一的问题是使用正确的ID导航到详细信息页面。这是我尝试过的:

package com.abc;
import static com.abc.Settings.SENDER_ID;
import java.net.URLEncoder;
import java.util.Random;
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.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gcm.GCMBaseIntentService;


public class GCMIntentService extends GCMBaseIntentService {

    private static final String TAG = "MyTag";
    static int i=0,j=0,k=0,l=0,m=0;
    Operation op=new Operation();
    Activity activity;

    public GCMIntentService() 
    {
        super(SENDER_ID);  string. 
    }
    @Override

    protected void onRegistered(Context context,String registrationId) 
    {
        Log.d(TAG, "Device registered :"+registrationId);

        op.register(getApplicationContext(), Settings.name, Settings.email, registrationId,Settings.staff_id);

    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.d(TAG, "Device unregistered :"+registrationId);

        op.unregister(getApplicationContext(), registrationId);
    }

    @Override
    protected void onMessage(Context context, Intent intent) 
    { 

        Log.d("In onMessage","In onMessage");

       String data = intent.getExtras().getString("message");//TOTAL ARRAY
       String hash = intent.getExtras().getString("hash");
       String title_data = intent.getExtras().getString("event");
       String client_id = intent.getExtras().getString("client_id");
       String department_id = intent.getExtras().getString("department_id");
       String ticket_id = intent.getExtras().getString("ticket_id");

       String message=getMessage(title_data);

       Log.d(TAG, "message :"+message);
       String id = null ,title = null;

      generateNotification(getApplicationContext(),message,title,id,title_data,hash,client_id,department_id,ticket_id);


    }

    // return text according to event 
    private String getMessage(String event) {

        // TODO Auto-generated method stub
        String message = null;
        if(event.equals("1"))
        {message=(String)getText(R.string.new_ticket_client);}
        else if(event.equals("3"))
        {message=(String)getText(R.string.reply_ticket_client);}
        else if(event.equals("4"))
        {message=(String)getText(R.string.client_added);}
        else if(event.equals("5"))
        {message=(String)getText(R.string.new_blab);}
        else if(event.equals("6"))
        {message=(String)getText(R.string.blab_comment);}
        return message;
    }
    @Override
    protected void onDeletedMessages(Context context, int total) 
    {

    }
    // On receiving an error
    @Override
    public void onError(Context context, String errorId) {
        Toast.makeText(getApplicationContext(),"Received error: " + errorId, Toast.LENGTH_SHORT).show();
        Log.d(TAG,"Received error: " + errorId);
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) 
    {
        Toast.makeText(getApplicationContext(),"Received recoverable error: " + errorId, Toast.LENGTH_SHORT).show();
        Log.d(TAG,"Received recoverable error: " + errorId);
        return super.onRecoverableError(context, errorId);
    }
   // method to generate a notification
    private static void generateNotification(Context context, String message,String title,String id,String event,String hash,String client_id,String department_id,String ticket_id) 
    {

            int icon = R.drawable.app_icon;
            long time = System.currentTimeMillis();

            NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

            Intent notificationIntent = null;

            if(event.equals("1"))
            {
                 i++;
                try{


                String msg="New ticket created by client ("+i+")";
                notificationIntent = new Intent(context,Tabs.class);
                notificationIntent.putExtra("filter_id","&vis_filter_id=1");        
                notificationIntent.putExtra("title",(String)context.getString(R.string.inbox));
                notificationIntent.putExtra("client_id",client_id);
                notificationIntent.putExtra("dept_id",department_id);
                notificationIntent.putExtra("set_queue","no");
                notificationIntent.putExtra("Ticket_hash",hash);
                notificationIntent.putExtra("Ticket_id",ticket_id);  
                notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);

               PendingIntent pending_intent =PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
               //PendingIntent pending_intent = PendingIntent.getBroadcast(context, 0,   notificationIntent, 0);
               // PendingIntent pending_intent =PendingIntent.getActivity(context, 0, notificationIntent, i);

                      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                        .setWhen(time)
                        .setContentTitle((String)context.getString(R.string.app_name))  
                        .setContentText(msg)
                        .setSmallIcon(icon)
                        .setAutoCancel(true)                    
                        .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND)
                        .setContentIntent(pending_intent); 

                      Notification notification=notificationBuilder.build();
                      notificationManager.notify(i, notification);

               // notificationManager.notify(0, notification);

                }
                catch(Exception e)
                {
                    Log.d("Exception in Notification : ",e.toString());
                }
            }


    }
}

1 个答案:

答案 0 :(得分:0)

尝试添加此

String x_id = UUID.randomUUID().toString();
notificationIntent.putExtra("x_id", x_id );
notificationIntent.setAction(x_id);