gpt4 book ai didi

android - 如何通过 SharedPreferences 保存解析的推送通知

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:46:57 24 4
gpt4 key购买 nike

我正在从 pasrse.com 发送推送通知,我想保存推送通知,以便稍后在应用程序关闭后可以看到它们。

我能够在单独 Activity 的 TextView 中接收通知,但如果我转到另一个 Activity ,通知就会消失。

主 Activity .java

public class MainActivity extends ActionBarActivity {
Button Notify;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Notify = (Button)findViewById(R.id.notify);


Notify.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Intent intent = new Intent(MainActivity.this, DisplayPush.class);
intent.putExtra("com.parse.Data","String text");
startActivity(intent);

}
});
}

显示推送.java

public class DisplayPush extends Activity {
String jsonData;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Titletobeput = "title";
public static final String Messagetobeput = "mess";

SharedPreferences sharedpreferences;
@Override
public void onBackPressed(){
Intent myIntent = new Intent(DisplayPush.this,MainActivity.class);
startActivity(myIntent);
// overridePendingTransition(R.anim.from_middle, R.anim.to_middle);
finish();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);

TextView notification_title = (TextView) findViewById(R.id.title);
TextView notification_message = (TextView) findViewById(R.id.message);


ParseAnalytics.trackAppOpened(getIntent());

Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(extras !=null)
if(extras.containsKey("com.parse.Data"))
jsonData = extras.getString("com.parse.Data");


//Bundle extras = intent.getExtras();
String jsonData = extras.getString("com.parse.Data");

try{
JSONObject notification = new JSONObject(jsonData);
String Title = notification.getString("title");
String Message = notification.getString("alert");


notification_message.setText(Message);
notification_title.setText(Title);

sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);



SharedPreferences.Editor editor = sharedpreferences.edit();

editor.putString(Titletobeput, Title);
editor.putString(Messagetobeput, Message);
editor.commit();
}
catch(JSONException e){
Toast.makeText(getApplicationContext(), "Something went wrong with the notification", Toast.LENGTH_SHORT).show();
}


}
}

Notification.xml(显示通知的位置)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:id="@+id/title"
android:layout_width="fill_parent"

android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="40dp" />

<TextView
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="22dp"
android:layout_marginTop="80dp" />

</RelativeLayout>

ActivityMain.xml(只是一个显示通知按钮的按钮)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="hasan.parsereceiveandshow.MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press the button to display notifications" />

<Button
android:id="@+id/notify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="44dp"
android:layout_marginTop="63dp"
android:text="See Notifications" />

</RelativeLayout>

有什么方法可以自动在状态栏/通知对话框中显示完整的通知吗?随着长消息被截断。

最佳答案

我想你使用的是最新版本的 Parse Library (Parse 1.9.3) 这就是你需要做的

您需要创建一个扩展 ParsePushBroadcastReceiver 的类,即 PushNotificationBroadcastReceiver 并覆盖 onReceive,如下所示:

    public class PushNotificationBroadcastReceiver extends ParsePushBroadcastReceiver {

/**
* Everytime a Push notification message comes, It is received here.
*
* @param context
* @param intent contains pushed Json or Message
*/
@Override
public void onReceive(Context context, Intent intent) {

Bundle extras = intent.getExtras();
try {
JSONObject notification;
notification = new JSONObject(extras.getString(ParsePushBroadcastReceiver.KEY_PUSH_DATA));
// Save Json to Shared Preferences. Add your Own implementation here
String title = notification.getString("title");
String message = notification.getString("alert");

SharedPreferences sharedpreferences = context.getSharedPreferences("myPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("title", title);
editor.putString("message", message);
editor.commit();
//----------------------------------------------------------------------------------

} catch (JSONException e) {
Log.e("Error Parsing JSON", e.getMessage());
}

}

/**
* Returns the Activity Class which you want to open when notification is tapped
*
* @param context
* @param intent
* @return Activity Class which you want to open
*/
@Override
protected Class<? extends Activity> getActivity(Context context, Intent intent) {
return SomeActivity.class;
}

/**
* This method is called once user taps on a Push Notification
*
* @param context
* @param intent
*/
@Override
protected void onPushOpen(Context context, Intent intent) {
super.onPushOpen(context, intent);
}

}

另外不要忘记像这样在您的 list 文件中注册一个PushNotificationBroadcastReceiver

        <receiver
android:name="com.package.PushNotificationBroadcastReceiver"
android:exported="false">

<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>

仅此而已,如果您需要更进一步,那么我建议您查看 Parse Push Notification documentation

希望这有帮助:)

关于android - 如何通过 SharedPreferences 保存解析的推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31204607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com