ian*_*jah 6 android firebase firebase-cloud-messaging
我已经尝试了这3天的android通知工作,用户点击通知然后活动打开.但每次我举杯,它都说是空的.尝试使用SOF的一些解决方案,但不起作用.你能看看代码有什么问题吗?提前致谢.
通知代码是
private void sendPushNotification(JSONObject json) {
//optionally we can display the json into log
int notificationId = new Random().nextInt();
Log.e(TAG, "Notification JSON " + json.toString());
try {
//getting the json data
JSONObject data = json.getJSONObject("data");
//parsing json data
String title = data.getString("title");
String message = data.getString("message");
String imageUrl = data.getString("image");
// Instantiate a Builder object.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this.getApplicationContext(),"Default");
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.putExtra("fromNotification", true);
// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// Creates the PendingIntent
PendingIntent pendingIntent =
PendingIntent.getActivity(
this.getApplicationContext(), notificationId,
notifyIntent,
PendingIntent.FLAG_ONE_SHOT
);
//int id = 1;
// Puts the PendingIntent into the notification builder
builder.setContentIntent(pendingIntent);
// Notifications are issued by sending them to the
// NotificationManager system service.
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Builds an anonymous Notification object from the builder, and
// passes it to the NotificationManager
if (mNotificationManager != null) {
mNotificationManager.notify(notificationId, builder.build());
}
} catch (JSONException e) {
Log.e(TAG, "Json Exception: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
而mainActivity.class是
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setElevation(0);
}
// Open Tab
if(getIntent().getExtras() != null){
Bundle b = getIntent().getExtras();
boolean cameFromNotification = b.getBoolean("fromNotification");
Toast.makeText(this.getApplicationContext(),
"Error: " + getIntent().getExtras(),
Toast.LENGTH_LONG).show();
viewPager = findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = findViewById(R.id.tabs);
viewPager.setCurrentItem(1);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
else {
viewPager = findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = findViewById(R.id.tabs);
viewPager.setCurrentItem(0);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
} }
Run Code Online (Sandbox Code Playgroud)
如果您已将额外内容添加到意图中并且在接收活动中未收到,则可能有 2 个原因。
该活动已经存在于 android backstack 中,并且额外的内容没有在 onCreate() 中捕获,但可以在 onNewIntent()
如果附加项通过 PendingIntent 的活动传递,则按照官方文档。http://developer.android.com/reference/android/app/PendingIntent.html。因此,要正确传递附加内容,您需要确保每个意图在action、data、type、class和Categories方面都有差异。或者使用FLAG_CANCEL_CURRENT或FLAG_UPDATE_CURRENT取消系统中存在的当前 PendingIntent 。
谢谢大家,我终于能够按照这篇文章让它工作了
我只使用通知,禁用从我的 php 服务器发送的通知并仅使用数据就可以了,我可以发送通知,并且 mainActivity 确实从意图中捕获 Putextra。
| 归档时间: |
|
| 查看次数: |
1983 次 |
| 最近记录: |