3

在我的应用程序中,我想使用第三方推送 woosh 发送推送通知。我正在使用 jar 文件。当我运行应用程序时,它显示注册状态和注册 ID,但如果我推送消息,它们不会显示在手机中。

     public class MainActivity extends FragmentActivity implements          SendTagsCallBack
          {
    private static final String SEND_TAGS_STATUS_FRAGMENT_TAG ="send_tags_status_fragment_tag";

    private static final String APP_ID = "554B5-9B410";
    private static final String SENDER_ID = "944240090087";

    private TextView mTagsStatus;
    private EditText mIntTags;
    private EditText mStringTags;
    private Button mSubmitTagsButton;
    private TextView mGeneralStatus;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //NetworkUtils.useSSL = true;

        PushManager pushManager = new PushManager(this, APP_ID, SENDER_ID);
        pushManager.onStartup(this);

        mGeneralStatus = (TextView) findViewById(R.id.general_status);
        mTagsStatus = (TextView) findViewById(R.id.status);
        mIntTags = (EditText) findViewById(R.id.tag_int);
        mStringTags = (EditText) findViewById(R.id.tag_string);

        checkMessage(getIntent());

        mSubmitTagsButton = (Button) findViewById(R.id.submit_tags);
        mSubmitTagsButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                checkAndSendTagsIfWeCan();


            }
        });

        SendTagsFragment sendTagsFragment = getSendTagsFragment();
        mTagsStatus.setText(sendTagsFragment.getSendTagsStatus());
        mSubmitTagsButton.setEnabled(sendTagsFragment.canSendTags());
    }

    /**
     * Called when the activity receives a new intent.
     */
    public void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);

        checkMessage(intent);
    }

    @Override
    public void onStatusChange(int sendTagsStatus)
    {
        mTagsStatus.setText(sendTagsStatus);
    }

    @Override
    public void onTaskEnds()
    {
        mSubmitTagsButton.setEnabled(true);
    }

    @Override
    public void onTaskStarts()
    {
        mSubmitTagsButton.setEnabled(false);
    }

    private void checkAndSendTagsIfWeCan()
    {
        SendTagsFragment sendTagsFragment = getSendTagsFragment();


        if (sendTagsFragment.canSendTags())
        {
            sendTagsFragment
                    .submitTags(this, mIntTags.getText().toString().trim(), mStringTags.getText().toString().trim());
        }
    }

    /**
     * Will check PushWoosh extras in this intent, and fire actual method
     *
     * @param intent activity intent
     */
    private void checkMessage(Intent intent)
    {
        if (null != intent)
        {
            if (intent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))
            {
                doOnMessageReceive(intent.getExtras().getString(PushManager.PUSH_RECEIVE_EVENT));
            }
            else if (intent.hasExtra(PushManager.REGISTER_EVENT))
            {
                doOnRegistered(intent.getExtras().getString(PushManager.REGISTER_EVENT));
            }
            else if (intent.hasExtra(PushManager.UNREGISTER_EVENT))
            {
                doOnUnregisteredError(intent.getExtras().getString(PushManager.UNREGISTER_EVENT));
            }
            else if (intent.hasExtra(PushManager.REGISTER_ERROR_EVENT))
            {
                doOnRegisteredError(intent.getExtras().getString(PushManager.REGISTER_ERROR_EVENT));
            }
            else if (intent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT))
            {
                doOnUnregistered(intent.getExtras().getString(PushManager.UNREGISTER_ERROR_EVENT));
            }

            resetIntentValues();
        }
    }

    public void doOnRegistered(String registrationId)
    {
        mGeneralStatus.setText(getString(R.string.registered, registrationId));
    }

    public void doOnRegisteredError(String errorId)
    {
        mGeneralStatus.setText(getString(R.string.registered_error, errorId));
    }

    public void doOnUnregistered(String registrationId)
    {
        mGeneralStatus.setText(getString(R.string.unregistered, registrationId));
    }

    public void doOnUnregisteredError(String errorId)
    {
        mGeneralStatus.setText(getString(R.string.unregistered_error, errorId));
    }

    public void doOnMessageReceive(String message)
    {
        mGeneralStatus.setText(getString(R.string.on_message, message));

    }

    /**
     * Will check main Activity intent and if it contains any PushWoosh data, will clear it
     */
    private void resetIntentValues()
    {
        Intent mainAppIntent = getIntent();

        if (mainAppIntent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))
        {
            mainAppIntent.putExtra(PushManager.PUSH_RECEIVE_EVENT, (String) null);
        }
        else if (mainAppIntent.hasExtra(PushManager.REGISTER_EVENT))
        {
            mainAppIntent.putExtra(PushManager.REGISTER_EVENT, (String) null);
        }
        else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_EVENT))
        {
            mainAppIntent.putExtra(PushManager.UNREGISTER_EVENT, (String) null);
        }
        else if (mainAppIntent.hasExtra(PushManager.REGISTER_ERROR_EVENT))
        {
            mainAppIntent.putExtra(PushManager.REGISTER_ERROR_EVENT, (String) null);
        }
        else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT))
        {
            mainAppIntent.putExtra(PushManager.UNREGISTER_ERROR_EVENT, (String) null);
        }

        setIntent(mainAppIntent);
    }

    private SendTagsFragment getSendTagsFragment()
    {
        FragmentManager fragmentManager = getSupportFragmentManager();
        SendTagsFragment sendTagsFragment =
                (SendTagsFragment) fragmentManager.findFragmentByTag(SEND_TAGS_STATUS_FRAGMENT_TAG);

        if (null == sendTagsFragment)
        {
            sendTagsFragment = new SendTagsFragment();
            sendTagsFragment.setRetainInstance(true);
            fragmentManager.beginTransaction().add(sendTagsFragment, SEND_TAGS_STATUS_FRAGMENT_TAG).commit();
            fragmentManager.executePendingTransactions();
        }

        return sendTagsFragment;
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();

        mIntTags = null;
        mStringTags = null;
        mTagsStatus = null;
        mSubmitTagsButton = null;
    }
}

     /*SendTagsFragment*/

          public class SendTagsFragment extends Fragment implements SendPushTagsCallBack
          {
    private final Object mSyncObject = new Object();

    private int mSendTagsStatus = R.string.status_ready;
    private AsyncTask<Void, Void, Void> mTask;

    public SendTagsFragment()
    {
    }

    public boolean canSendTags()
    {
        synchronized (mSyncObject)
        {
            return mTask == null;
        }
    }

    public void submitTags(final Context context, String tagInt, String tagString)
    {
        synchronized (mSyncObject)
        {
            if (!canSendTags())
            {
                return;
            }

            if (!goodAllInputData(tagInt, tagString))
            {
                return;
            }

            mSendTagsStatus = R.string.status_started;
            transfareTaskStartsToActivity();

            final Map<String, Object> tags = generateTags(tagInt, tagString);

            mTask = new AsyncTask<Void, Void, Void>()
            {
                @Override
                protected Void doInBackground(Void... params)
                {
                    PushManager.sendTags(context, tags, SendTagsFragment.this);

                    return null;
                }
            };
            mTask.execute((Void) null);
        }
    }

    public int getSendTagsStatus()
    {
        synchronized (mSyncObject)
        {
            return mSendTagsStatus;
        }
    }

    @Override
    public void taskStarted()
    {
        synchronized (mSyncObject)
        {
            mSendTagsStatus = R.string.status_started;
            transfareStatusToActivity();
        }
    }

    @Override
    public void onSentTagsSuccess(Map<String, String> stringStringMap)
    {
        synchronized (mSyncObject)
        {
            mSendTagsStatus = R.string.status_success;
            mTask = null;
            transfareStatusToActivity();
            transfareTaskEndsToActivity();
        }
    }

    @Override
    public void onSentTagsError(PushWooshException e)
    {
        synchronized (mSyncObject)
        {
            mSendTagsStatus = R.string.status_error;
            if (null != e)
            {
                e.printStackTrace();
            }
            mTask = null;
            transfareStatusToActivity();
            transfareTaskEndsToActivity();
        }
    }

    private boolean goodAllInputData(String tagInt, String tagString)
    {
        if (tagInt.length() == 0 && tagString.length() == 0)
        {
            mSendTagsStatus = R.string.status_init_error;
            transfareStatusToActivity();
            transfareTaskEndsToActivity();
            return false;
        }
        if (tagInt.length() != 0)
        {
            try
            {
                Integer.parseInt(tagInt);
            }
            catch (Exception e)
            {
                mSendTagsStatus = R.string.status_int_parse_error;
                transfareStatusToActivity();
                transfareTaskEndsToActivity();
                return false;
            }
        }
        return true;
    }

    private void transfareTaskStartsToActivity()
    {
        SendTagsCallBack sendTagsCallBack = (SendTagsCallBack) getActivity();
        if (null != sendTagsCallBack)
        {
            sendTagsCallBack.onTaskStarts();
        }
    }

    private void transfareTaskEndsToActivity()
    {
        SendTagsCallBack sendTagsCallBack = (SendTagsCallBack) getActivity();
        if (null != sendTagsCallBack)
        {
            sendTagsCallBack.onTaskEnds();
        }
    }

    private void transfareStatusToActivity()
    {
        SendTagsCallBack sendTagsCallBack = (SendTagsCallBack) getActivity();
        if (null != sendTagsCallBack)
        {
            sendTagsCallBack.onStatusChange(mSendTagsStatus);
        }
    }

    private Map<String, Object> generateTags(String tagInt, String tagString)
    {
        Map<String, Object> tags = new HashMap<String, Object>();

        if (tagInt.length() != 0)
        {
            tags.put("FavNumber", Integer.parseInt(tagInt));
        }
        if (tagString.length() != 0)
        {
            tags.put("Alias", tagString);
        }

        return tags;
    }
   }

I gave these permissions in Manifest file

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.VIBRATE"/>


 <!--library-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET"/>

    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <!--
     Creates a custom permission so only this app can receive its messages.

     NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
           where PACKAGE is the application's package name.
    -->
    <permission
            android:name="com.pushwoosh.test.tags.sample.app.permission.C2D_MESSAGE"
            android:protectionLevel="signature"/>
    <uses-permission
            android:name="com.pushwoosh.test.tags.sample.app.permission.C2D_MESSAGE"/>

    <!-- This app has permission to register and receive data message. -->
    <uses-permission
            android:name="com.google.android.c2dm.permission.RECEIVE"/>


       <intent-filter>
                <action android:name="com.pushwoosh.test.tags.sample.app.MESSAGE"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>

        </activity>

        <activity android:name="com.arellomobile.android.push.PushWebview"/>

        <activity android:name="com.arellomobile.android.push.MessageActivity"/>

        <activity android:name="com.arellomobile.android.push.PushHandlerActivity"/>


        <!--
          BroadcastReceiver that will receive intents from GCM
          services and handle them to the custom IntentService.

          The com.google.android.c2dm.permission.SEND permission is necessary
          so only GCM services can send data messages for the app.
        -->
        <receiver
                android:name="com.google.android.gcm.GCMBroadcastReceiver"
                android:permission="com.google.android.c2dm.permission.SEND"
                >
            <intent-filter>
                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
                <category android:name="com.pushwoosh.test.tags.sample.app"/>
            </intent-filter>
        </receiver>

        <!--
          Application-specific subclass of PushGCMIntentService that will
          handle received messages.
        -->
        <service android:name="com.arellomobile.android.push.PushGCMIntentService"/>

        <!--
          Service for sending location updates
        -->
        <service android:name="com.arellomobile.android.push.GeoLocationService"/>
4

1 回答 1

2

在 1]Sender_Id 中输入您的“GCM App ID”,在 2]在 APP ID 中正确输入您的“PushWoosh APP KEY”,然后登录后进入 PushWoosh 网站,进入应用程序部分,然后输入您的消息。

于 2013-01-30T06:04:51.383 回答