繁体   English   中英

使用GCM和Asp.Net服务器推送通知

[英]Push Notification Using GCM with Asp.Net server

我已经为我的Android应用程序使用GCM实现了推送通知。 现在,我收到所有已注册设备的通知。 但是我应该何时注销设备感到困惑。

MainActivity.java

public class DemoActivity extends Activity {

    TextView mDisplay;
    private String TAG = "** Demo Activity Push **";

    public final String SOAP_ACTION4 = "http://tempuri.org/InsertDeviceID";
    public final String OPERATION_NAME4 = "InsertDeviceID"; 
    public final String WSDL_TARGET_NAMESPACE4 = "http://tempuri.org/";
    public final String SOAP_ADDRESS4 = "http://www.icecup.com/iphonews/pushnotification.asmx"; 

    String regId ;
    String receivedString;
    String delreceivedString;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        checkNotNull(SENDER_ID, "SENDER_ID");

        GCMRegistrar.checkDevice(getApplicationContext()); 
        GCMRegistrar.checkManifest(getApplicationContext());
           setContentView(R.layout.main);
        mDisplay = (TextView) findViewById(R.id.display);     
        regId = GCMRegistrar.getRegistrationId(getApplicationContext());
        if (regId.equals("")) {
            // Automatically registers application on startup.
            GCMRegistrar.register(getApplicationContext(), SENDER_ID);
        } else {
        Log.v(TAG, "Already registered");           
        }
       mDisplay.setText("ffffff        "+regId);
     //   setDeviceRegId(regId);
       // new MovieDetails().execute(regId);
       try {
            Class.forName("android.os.AsyncTask");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

    public class MovieDetails extends AsyncTask<String, Void, Void> {

        @Override
        protected  Void doInBackground(String... params) {          

            String parregid= params[0];
            Log.d("DEVICE REG ID", parregid);
            SoapObject request99 = new SoapObject(WSDL_TARGET_NAMESPACE4,OPERATION_NAME4);
            request99.addProperty("DeviceId",parregid);
            System.out.println("I AM ALIVE 1");
                SoapSerializationEnvelope envelope99 = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);

                envelope99.dotNet = true;

                envelope99.setOutputSoapObject(request99);
                System.out.println("I AM ALIVE 2");
                HttpTransportSE httpTransport99 = new HttpTransportSE(SOAP_ADDRESS4);
                System.out.println("I AM ALIVE 3");             
                SoapPrimitive  response=null;
                try {
                    httpTransport99.call(SOAP_ACTION4, envelope99);
                    System.out.println("I AM ALIVE 4");
                    response = (SoapPrimitive) envelope99.getResponse();    
                    System.out.println("I AM ALIVE 5");
                    System.out.println("RESPONSE IS:  "+response.toString());   
                    receivedString=response.toString();
                } catch (Exception e) {
                }
                return null;
        }

        @Override
        protected void onCancelled()
        {
            super.onCancelled();
        }

        @Override
        protected void onPreExecute() {

            System.out.println("I AM ALIVE 0");
        }

        @Override
        protected void onPostExecute( Void result) {        

            System.out.println(" RESPONSE "+receivedString);
            super.onPostExecute(result);
        }


    }

    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String newMessage = intent.getExtras().getString("message");
            // Waking up mobile if it is sleeping
            WakeLocker.acquire(getApplicationContext());            

            mDisplay.append(newMessage + "\n");         
            Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();            
            WakeLocker.release();
        }
    };

    @Override
    protected void onDestroy() {

        try {
            unregisterReceiver(mHandleMessageReceiver);
             GCMRegistrar.onDestroy(getApplicationContext());

        } catch (Exception e) {
            Log.e("UnRegister Receiver Error", "> " + e.getMessage());
        }
        super.onDestroy();
    }

    private void checkNotNull(Object reference, String name) {
        if (reference == null) {
            throw new NullPointerException(
                    getString(R.string.error_config, name));
        }
    }
}

这是我的主要活动。 告诉我应该在哪里打电话给GCM.Unregister(this)。 因为如果有人卸载了该应用程序,则该注册ID的GCM服务器会将响应NotRegistered发送给第三方服务器。 因此,请记住这一点,我正在考虑注销该设备。

GCMIntentService.java

public class GCMIntentService extends GCMBaseIntentService {


    private static final String TAG = "GCMIntentService";
    String installregids = "";
    String uninstallregids = "";
    DemoActivity dact= new DemoActivity();

    public GCMIntentService() {
        super(SENDER_ID);
    }

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

        this.installregids=registrationId;
         try {
        dact.new MovieDetails().execute(registrationId); 


           GCMRegistrar.setRegisteredOnServer(context, true);
         }
         catch (Exception e) {
             e.printStackTrace();
         }
    }

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

        this.uninstallregids=registrationId; 
         try {
             if (GCMRegistrar.isRegisteredOnServer(context)) {
            //  dact.new MessgaeDelete().execute(registrationId);
                } else {

                    Log.i(TAG, "Ignoring unregister callback");
                }

           GCMRegistrar.setRegisteredOnServer(context, false);
         }
         catch (Exception e) {
             e.printStackTrace();
         }
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
      //  String message = getString(R.string.gcm_message);
        String message = intent.getExtras().getString("message");
        Log.d("Message Received", message);

        generateNotification(context, message);
    }


    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        // log message
        Log.i(TAG, "Received recoverable error: " + errorId);

        return super.onRecoverableError(context, errorId);
    }

    private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_stat_gcm;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = context.getString(R.string.app_name);
        Intent notificationIntent = new Intent(context, DemoActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }

}

请帮助我解决此问题。

您通常不调用GCMRegistrar.unregister() 如果您卸载该应用程序,则会自动“取消注册”。 我认为此方法的目的仅是为了允许从应用程序中进行显式注销。 例如,如果用户可以完全关闭自动同步功能,则无需注册。 您也不会收到有关该应用程序正在卸载的事实的通知。 该应用程序永远不会注意到,因为没有诸如“ preUninstall”之类的东西。

暂无
暂无

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

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