配置AndroidManifest.xml
<permission
android:name="在極光註冊的包名.permission.JPUSH_MESSAGE"
android:protectionLevel="signature" />
<!-- Required 一些系統要求的權限,如訪問網絡等 -->
<uses-permission android:name="在極光註冊的包名.permission.JPUSH_MESSAGE" />
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application
android:name="自己的Application"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.sunset.im.IMActivity" >
</activity>
<activity android:name="在極光註冊的包名.DengluActivity"></activity>
<!-- Required SDK 核心功能 -->
<!-- option since 2.0.5 可配置PushService,DaemonService,PushReceiver,AlarmReceiver的android:process參數 將JPush相關組件設置爲一個獨立進程 -->
<!-- 如:android:process=":remote" -->
<service
android:name="cn.jpush.android.service.PushService"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTER" />
<action android:name="cn.jpush.android.intent.REPORT" />
<action android:name="cn.jpush.android.intent.PushService" />
<action android:name="cn.jpush.android.intent.PUSH_TIME" />
</intent-filter>
</service>
<!-- since 1.1.4 option 可選項。用於同一設備中不同應用的JPush服務相互拉起的功能。 -->
<!-- 若不啓用該功能可刪除該組件,將不拉起其他應用也不能被其他應用拉起 -->
<service
android:name="cn.jpush.android.service.DaemonService"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="cn.jpush.android.intent.DaemonService" />
<category android:name="在極光註冊的包名" />
</intent-filter>
</service>
<!-- Required Push SDK核心功能 -->
<receiver
android:name="cn.jpush.android.service.PushReceiver"
android:enabled="true" >
<intent-filter android:priority="1000" >
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
<category android:name="在極光註冊的包名" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<!-- Optional -->
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<!-- Required Push SDK核心功能 -->
<activity
android:name="cn.jpush.android.ui.PushActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="在極光註冊的包名" />
</intent-filter>
</activity>
<!-- Required Push SDK核心功能 -->
<service
android:name="cn.jpush.android.service.DownloadService"
android:enabled="true"
android:exported="false" />
<!-- Required Push SDK核心功能 -->
<receiver android:name="cn.jpush.android.service.AlarmReceiver" />
<!-- IM Required IM SDK核心功能 -->
<receiver
android:name="cn.jpush.im.android.helpers.IMReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter android:priority="1000" >
<action android:name="cn.jpush.im.android.action.IM_RESPONSE" />
<action android:name="cn.jpush.im.android.action.NOTIFICATION_CLICK_PROXY" />
<category android:name="在極光註冊的包名" />
</intent-filter>
</receiver>
<!-- Required. Enable it you can get statistics data with channel -->
<meta-data
android:name="JPUSH_CHANNEL"
android:value="developer-default" />
<!-- Required. AppKey copied from Portal -->
<meta-data
android:name="JPUSH_APPKEY"
android:value="在極光註冊的的應用的APPKEY" />
先初始化Application
@Override
public void onCreate() {
super.onCreate();
Log.i("JMessageDemoApplication", "Application onCreate");
JMessageClient.init(getApplicationContext());
//是否打印LOG
JPushInterface.setDebugMode(true);
}
註冊用戶
public void register() {
JMessageClient.register("用戶名", "用戶密碼", new BasicCallback() {
@Override
public void gotResult(int arg0, String arg1) {
if (arg0==0) {
Toast.makeText(MainActivity.this, "註冊成功", 0).show();
}
}
});
}
獲取用戶信息
public void getUserInfo() {
JMessageClient.getUserInfo(FriendName, new GetUserInfoCallback() {
@Override
public void gotResult(int arg0, String arg1, UserInfo arg2) {
System.out.println("............." + arg0);
System.out.println("............." + arg1);
System.out.println(arg2.getUserName() + ">>>>>>>>>>>>>"+ arg2.getNickname());
}
});
}
一定要在使用極光SDK的Activity重配置onResume()和onPause()
@Override
protected void onResume() {
super.onResume();
JPushInterface.onResume(this);
}
@Override
protected void onPause() {
super.onPause();
JPushInterface.onPause(this);
}
登錄
public void login() {
JMessageClient.login(dl_name.getText().toString(), dl_pwd.getText().toString(), new BasicCallback() {
@Override
public void gotResult(int arg0, String arg1) {
if (arg0==0) {
Toast.makeText(DengluActivity.this, "登陸成功", 0).show();
}
}
});
}
發送消息要在開啓一個子線程
//創建一個消息對象
Message m = JMessageClient.createSingleTextMessage("朋友的用戶名", "要發送的消息");
//發送消息
JMessageClient.sendMessage(m);
接受消息
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_im);
//註冊一個接受的廣播
JMessageClient.registerEventReceiver(this);
}
//接受消息的事件
public void onEventMainThread(MessageEvent event) {
Message msg = event.getMessage();
switch (msg.getContentType()) {
case text:
// 處理文字消息
TextContent textContent = (TextContent) msg.getContent();
textContent.getText();
txt_sms.append(textContent.getText());
break;
}
}
一定要在onDestory()中去掉廣播
@Override
protected void onDestroy() {
super.onDestroy();
JMessageClient.unRegisterEventReceiver(this);
}
自定義設置通知欄的跳轉界面,默認的是跳轉主界面
public void onEvent(NotificationClickEvent event) {
Intent notificationIntent = new Intent(this, IMActivity.class);
this.startActivity(notificationIntent);// 自定義跳轉到指定頁面
}